簡體   English   中英

我如何在python中彼此分離2個輸出

[英]How do I seperate 2 outputs from each other in python

gate = input("Choose\nA) for 'AND' gate and\nB) for 'OR' gate\n")

if gate == "A":
    val1 = float(input("Whats your A value?"))
    val2 = float(input("Whats your B value?"))

if val1 == 0 and val2 == 0:
    print("Your C value is 0")

if val1 == 0 and val2 == 1:
    print("Your C value is 0")

if val1 == 1 and val2 == 0:
    print("Your C value is 0")

if val1 == 1 and val2 == 1:
    print("Your C value is 1")

#Right here is the error, I need something to separate top process from bottom process

if gate == "B":
    val1 = float(input("Whats your A value?"))
    val2 = float(input("Whats your B value?"))

if val1 == 0 and val2 == 0:
    print("Your C value is 0")

if val1 == 0 and val2 == 1:
    print("Your C value is 1")

if val1 == 1 and val2 == 1:
    print("Your C value is 1")

if val1 == 1 and val2 == 0:
    print("Your C value is 1")

因此一切正常,如果我只是將if gate == "A":部分而不是if gate == "B":部分放入,它將正常工作。 根據我在C語言方面的經驗,我覺得我需要一些將兩個任務/流程彼此分開的東西,也許有人告訴我這行代碼,我已經在互聯網上搜索了很多地方,但仍然沒有找到解決方案。

為了分離這兩個過程,這可能是您想要做的:

def process_A():
    val1 = int(input("Whats your A value?"))
    val2 = int(input("Whats your B value?"))

    if val1 == 0 and val2 == 0:
        print("Your C value is 0")

    if val1 == 0 and val2 == 1:
        print("Your C value is 0")

    if val1 == 1 and val2 == 0:
        print("Your C value is 0")

    if val1 == 1 and val2 == 1:
        print("Your C value is 1")

def process_B():
    val1 = int(input("Whats your A value?"))
    val2 = int(input("Whats your B value?"))

    if val1 == 0 and val2 == 0:
        print("Your C value is 0")

    if val1 == 0 and val2 == 1:
        print("Your C value is 1")

    if val1 == 1 and val2 == 1:
        print("Your C value is 1")

    if val1 == 1 and val2 == 0:
        print("Your C value is 1")


gate = input("Choose\nA) for 'AND' gate and\nB) for 'OR' gate\n")

if gate == "A":
    process_A()
elif gate == "B":
    process_B()

輸出:

輸入A->一個過程:

Choose
A) for 'AND' gate and
B) for 'OR' gate
B
Whats your A value?0
Whats your B value?0
Your C value is 0

輸入A->一個過程:

Choose
A) for 'AND' gate and
B) for 'OR' gate
A
Whats your A value?1
Whats your B value?1
Your C value is 1
gate = raw_input("Choose\nA) for 'AND' gate and\nB) for 'OR' gate\n")

if gate == "A":
    val1 = input("Whats your A value?")
    val2 = input("Whats your B value?")

    if val1 == 1 and val2 == 1:
        print("Your C value is 1")
    else:
        print("Your C value is 0")


#Right here is the error, I need something to separate top process from bottom process

if gate == "B":
    val1 = input("Whats your A value?")
    val2 = input("Whats your B value?")
    if val1 == 0 and val2 == 0:
        print("Your C value is 0")
    else:
        print("Your C value is 1")

我更改了raw_input的輸入以獲取類似字符串的字母,並且壓縮了代碼,因為在具有相同響應時無需進行更多驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM