简体   繁体   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")

So everything works, If I just put the if gate == "A": section and not the if gate == "B": part it work. 因此一切正常,如果我只是将if gate == "A":部分而不是if gate == "B":部分放入,它将正常工作。 From my experience in C, I feel like I need something that seperates both tasks / processes from each other, may someone tell me the line of code, I've searched many places on the internet and still haven't found a solution. 根据我在C语言方面的经验,我觉得我需要一些将两个任务/流程彼此分开的东西,也许有人告诉我这行代码,我已经在互联网上搜索了很多地方,但仍然没有找到解决方案。

In order to separate the two processes, this is probably what you want to do: 为了分离这两个过程,这可能是您想要做的:

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()

output: 输出:

Input A --> A process: 输入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

Input A --> A process: 输入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")

I changed the input for raw_input for get the letter like string, and I compressed the code because no need more validation when have the same response. 我更改了raw_input的输入以获取类似字符串的字母,并且压缩了代码,因为在具有相同响应时无需进行更多验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何分隔每个列表项以减去该项目 python - How do I seperate each list item to minus that item, python 如何使Gnome面板小程序(用python编写)为每个实例存储单独的设置? - How do I make a Gnome Panel applet (written in python) store seperate settings for each instance? 如何从python列表生成CSV文件,并将每个列表项放在单独的行中 - How to I generate a CSV file from a python list with each list item in a seperate row 如何在Python中将不同函数的结果相加? - How do I subract results from different functions from each other in Python? 如何在 Python 中将一个数组中的多列堆叠在一起? - How do I stack multiple columns from one single array on top of each other in Python? 如何使用 python opencv 将重叠的卡彼此分开? - How do i separate overlapping cards from each other using python opencv? 如何使用python和opencv互相减去两个图像? - How do I subtract two images from each other using python and opencv? 如何从列表中减去列表? - How do I subtract lists from each other? 我如何将这些数据彼此分开 - How do i separate these data from each other 如何在python中分隔字段和路径gps坐标? - How do I seperate field and path gps coordinates in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM