我目前正在制作自己的冒险游戏。在代码中,有一个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')
当您处于“转向”状态时,它可以很好地工作,但是当您转到厨房部分并键入“转到厨房”时,它会打印出预期的内容(您去厨房,然后你爸爸问你要早餐吃什么,你想要培根还是鸡蛋?它还会打印出来(我不知道去厨房意味着什么)。
答案 0 :(得分:1)
您有缩进错误while True
将无限期运行。
如果要执行所有的if
,请放在while
中。
答案 1 :(得分:0)
您将while player_input not in actions
循环放入无限while True
循环中。这将导致您的内部循环无限期地运行。您所需要做的就是删除while True
循环,它应该可以工作:
while player_input not in actions:
print('i dont know what you mean by',player_input)
player_input = input('>')
或缩进循环中想要的所有内容。