Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:While循环无止境_Python - Fatal编程技术网

Python: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":

我正试图获得一个游戏编程类作业的条件性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 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可能需要检查是否有其他有效的输入。非常感谢。我想我需要重新思考我的代码,不要犯同样的错误。