Python:While循环无止境
我正试图获得一个游戏编程类作业的条件性while或语句。我正在尝试使用类似这样的循环。它们并不完全像这样,但while语句的条件是相同的Python:While循环无止境,python,Python,我正试图获得一个游戏编程类作业的条件性while或语句。我正在尝试使用类似这样的循环。它们并不完全像这样,但while语句的条件是相同的 tempstr = input("Type B ") while tempstr != "B" or tempstr != "b": tempstr = input("Type anything ") 我也试过了 tempstr = input("Type B ") while tempstr == "B" or tempstr == "b":
tempstr = input("Type B ")
while tempstr != "B" or tempstr != "b":
tempstr = input("Type anything ")
我也试过了
tempstr = input("Type B ")
while tempstr == "B" or tempstr == "b":
tempstr = input("Type anything ")
以及
while tempstr == "B" or tempstr == "b":
tempstr = input("Type anything ")
我检查了tempstr是否设置为B或B,他们仍然会继续请求输入,而不是仅仅结束程序。试试看
While True:
tempstr = input("Type anything ")
if tempstr.lower() == 'b':
break
为什么我们要添加一个
while True
包装,并在我们已经知道触发while中断的条件时将其中断?@sberry如果这是一个游戏,那么OP可能需要检查是否有其他有效的输入。非常感谢。我想我需要重新思考我的代码,不要犯同样的错误。