【问题标题】:While loop (really simple)While 循环(非常简单)
【发布时间】:2021-05-29 13:01:45
【问题描述】:

我正在编写下面的代码,我希望 temp 成为 12,但它会一直在 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)
    

    【讨论】:

    • cmon man...我刚刚在你面前发布了。
    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 2012-10-27
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多