如何在python中相互分离2个输出

时间:2016-04-11 13:59:25

标签: python process

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方面的经验,我觉得我需要一些能够将任务/流程彼此分开的东西,可能有人告诉我代码行,我在互联网上搜索了许多地方,但仍未找到解决方案。 / p>

2 个答案:

答案 0 :(得分:0)

为了分离这两个过程,这可能就是你想要做的事情:

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

答案 1 :(得分:0)

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的输入以获取字符串之类的字母,并且我压缩了代码,因为在具有相同响应时不需要更多验证。