Python 为什么即使运行while循环的条件不是';我没见过?

Python 为什么即使运行while循环的条件不是';我没见过?,python,Python,我目前正在制作一款自己制作的冒险游戏。在代码中,有一个while循环来检查运行游戏的人给出的输入是否有效 actions = ['turn light on'] while True: while player_input not in actions: print('i dont know what you mean by',player_input) player_input = input('>') if player_input == (

我目前正在制作一款自己制作的冒险游戏。在代码中,有一个while循环来检查运行游戏的人给出的输入是否有效

actions = ['turn light on']

while True:
    while player_input not in actions:
        print('i dont know what you mean by',player_input)
        player_input = input('>')

if player_input == ('turn light on'):
    actions.remove('turn light on')
    actions.append('go to kitchen')
    actions.append('turn light off')

    points = int(points+100)

    print('you have',points,'points')
    print('it is bright in your room')
    print('you hear your dad say something in the kitchen')

    player_input = input('>')

    if player_input == ('go to kitchen'):
        actions.remove('go to kitchen')
        actions.remove('turn light off')
        actions.append('eat eggs')
        actions.append('eat bacon')
        actions.append('eat dad')

        print('you go to the kitchen and your dad asks what you want for breakfast')
        print('do you want to eat bacon or eggs')

当你在“打开部分的灯”时,它工作正常,但当你进入“去厨房”部分并键入“去厨房”时,它会打印出它应该打印的内容(你去厨房,你爸爸问你早餐想要什么,你想要熏肉还是鸡蛋),但它也会打印(我不知道你说的去厨房是什么意思).

您有一个缩进错误
,而True
将无限期运行。
如果希望执行,则将所有
if
放入
while
中。

while player\u输入不在动作中时放入无限
循环,而为True
循环。这会导致内部循环无限期运行。您所要做的就是删除
while True
循环,它应该可以工作:

while player_input not in actions:
    print('i dont know what you mean by',player_input)
    player_input = input('>')
或者在循环中缩进您想要的所有内容


另请参见:

while True
从不中断,除非您明确使用
break
。在什么情况下,您希望第一个循环中断?您有
,而True
,没有任何东西可以中断-这样循环将永远运行
“去厨房”
不是列表中的成员
['turn light on']
,因此它会打印
“我不知道你说的是什么…”
,就像你在第二个
循环中告诉它的那样。不,它没有按你说的做。您发布的代码无法运行,因为
player\u input
未定义。您的缩进在某些地方也不正确,因为您无法访问您声称执行的
print
命令。@0x5453它甚至从未达到如此程度