如何使用python 3.7.x在if语句中检查两个条件

时间:2019-01-10 21:59:33

标签: python if-statement syntax

这是我的代码:

if response != (("1") or ("2")):
   while response != (("1") or ("2")):
       print("\nInvalid input, try again")
       print("\nWhat do you prefer \n 1) Fruit\n or\n 2) Vegetable?\n")
       response = input("Answer with 1 or 2: ")
       if response == (("1") or ("2")):
           break

如何在我的第一个if语句中检查两个条件     我想确保输入为"1""2"作为字符串。     如果没有,我将重复这个问题。     当前,代码if reponse != (("1") or ("2")):和其他if语句知道1是一个有效答案并继续前进,但是如果我输入2,它将进入循环并重复该问题,这是一个知道2是有效答案的问题。 。我如何在if语句中包括2? 我想知道2条件if语句的语法。

question = "What do you prefer between \n 1)fruits \n or \n 2)vegetables?"
print(question)
response = input("Answer with 1 or 2:")
#Loop_if_not_1_or_2_(not_an_avalaible_answer)
if response != ("1" or "2"):
    while response != ("1" or "2"):
        print("Invalid input, try again")
        print("What do you prefer\n \n 1) Fruit\n or\n 2) Vegetable?\n")
        response = input("Answer with 1 or 2: ")
        if response == ("1" or "2"):
            break


#didn't entered the loop, to next question
print("so guys we did it")

如果我输入2,我希望我的代码不进入循环。

0 个答案:

没有答案