我需要在while语句中使用多个条件

时间:2014-02-18 00:01:10

标签: python loops

我是编码的新手,我对自己在做什么一无所知。所以这可能是非常错误的。但我必须创建一个确定斜率和Y截距的代码。我创建了一个循环来确定是否有任何坐标相同,如果它们必须更改,但是我无法摆脱循环。这是我目前的循环。任何帮助将非常感激!提前谢谢!

def whileloop():

while True:
    y1 == y2 or x1 == x2


    print ("Error")




            print("please enter values that are not equal to eachother.")
            y1_str = input ("please enter y1: ")
            y2_str = input ("please enter y2: ")



            print ("please enter values that are not equal to eachother.")
            x1_str = input ("please enter x1: ")
            x2_str = input ("please enter x2: ")


else: y1!= y2 or x1!=x2

2 个答案:

答案 0 :(得分:0)

认为你想要的是这样的:

def whileloop():
    while True:
        y1_str = input("please enter y1: ")
        y2_str = input("please enter y2: ")
        x1_str = input("please enter x1: ")
        x2_str = input("please enter x2: ")
        y1 = float(y1_str)
        y2 = float(y2_str)
        x1 = float(x1_str)
        x2 = float(x2_str)

        if y1 == y2 or x1 == x2:
            print("please enter values that are not equal to eachother.")
        else:
            return y1, y2, x1, x2

答案 1 :(得分:0)

在while / if / elif语句中使用多个条件的方法非常简单。像这样的东西工作正常

while A and B:

while A or B: 

while A and not B:

while (A and B) or C:

可能性是无穷无尽的 - 只要确保清楚地定义每个条件,如果有疑问,为了清楚起见,用括号分开条件。具体为您的代码

while y1 == y2 or x1 == x2:
    print("please enter values that are not equal to eachother.")

    y1_str = input("please enter y1: ")
    y2_str = input("please enter y2: ")
    x1_str = input("please enter x1: ")
    x2_str = input("please enter x2: ")

    y1 = float(y1_str)
    y2 = float(y2_str)
    x1 = float(x1_str)
    x2 = float(x2_str)


# Do stuff with y1, y2, x1, x2

此方法要求您已经定义了y1y2x1x2 - 如果您想在while循环中定义它们,那么abarnert的方法将会工作得很好