【问题标题】:While loop (really simple)While 循环(非常简单)
【发布时间】:2021-05-29 13:01:45
【问题描述】:
我正在编写下面的代码,我希望 temp 成为 1 或 2,但它会一直在 while 循环中运行。
temp = input("1 for yes/2 for no")
while (temp!="1" or temp!="2"):
print("please input 1 or 2")
temp = input("1 for yes/2 for no")
print(temp)
【问题讨论】:
标签:
python
python-3.x
loops
while-loop
【解决方案1】:
改变这个:
while temp!="1" or temp!="2":
到这里:
while temp!="1" and temp!="2":
【解决方案2】:
您需要and 而不是or。
使用or,条件永远不会满足,因为temp不能同时是"1"和"2":
temp=input("1 for yes/2 for no")
while(temp!="1" and temp!="2"):
print("please input 1 or 2")
temp=input("1 for yes/2 for no")
print(temp)