Python While在函数中循环?
我对编码完全陌生,我正在使用Python编写一个基于文本的游戏,作为学校项目。用户必须通过键入“北”、“东”、“南”或“西”进入房间。如果方向无效,将弹出一条错误消息,提示用户输入新方向。如果用户键入“退出”,游戏将结束 这个项目有一百万个问题,因为我发现我在编码方面很糟糕,但我想弄清楚的是,如果出现提示,如何让我的程序退出游戏。这是我的代码(它不是完整的代码,只是我到目前为止所拥有的代码。我试图一步一步地解决问题,这是我遇到的问题): 运行程序时,我收到以下错误消息:Python While在函数中循环?,python,function,loops,Python,Function,Loops,我对编码完全陌生,我正在使用Python编写一个基于文本的游戏,作为学校项目。用户必须通过键入“北”、“东”、“南”或“西”进入房间。如果方向无效,将弹出一条错误消息,提示用户输入新方向。如果用户键入“退出”,游戏将结束 这个项目有一百万个问题,因为我发现我在编码方面很糟糕,但我想弄清楚的是,如果出现提示,如何让我的程序退出游戏。这是我的代码(它不是完整的代码,只是我到目前为止所拥有的代码。我试图一步一步地解决问题,这是我遇到的问题): 运行程序时,我收到以下错误消息: 如果有人能给我指出我需
如果有人能给我指出我需要解决的问题的正确方向,我将非常感激 因此,当用户输入
“退出”
时,您可以在房间中找到它。您应该将退出条件移动到输入之后,而不是成为while
条件的一部分:
房间={
“大厅”:{“南”:“卧室”},
‘卧室’:{‘北’:‘大厅’,‘东’:‘地窖’},
“地窖”:{“西部”:“卧室”}
}
def main():
当前会议室=‘大厅’
尽管如此:
打印('您在',当前房间+'。)
用户输入=输入('您想去哪里?:')
如果用户输入=“退出”:
打破
当前房间=房间[当前房间][用户输入]
打印(“感谢您的播放!”)
main()
是否尝试将其放入if语句中?发生错误是因为它试图在循环回while语句之前找到要移动的正确房间。使用if会立即强制它首先确定退出
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def main():
current_room = 'Great Hall'
user_input = None
while user_input != "Quit":
print('You are in the', current_room + '.')
user_input = input('Where would you like to go?: ')
if user_input == "Quit":
print('Thanks for playing!')
else:
current_room = rooms[current_room][user_input]
main()
您可以使用walrus运算符以获得更简单的代码:
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def main():
current_room = 'Great Hall'
while user_input:=input('Where would you like to go?: ') != 'Quit':
print('You are in the ' + current_room + '.')
current_room = rooms[current_room][user_input]
print('Thanks for playing!')
main()
错误是Quit键不在rooms字典中。我有一个版本的代码可以工作:
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def main():
current_room = 'Great Hall'
while True:
print('You are in the', current_room + '.')
user_input = input('Where would you like to go?: ')
if user_input == "Quit":
break
if user_input in rooms[current_room]:
current_room = rooms[current_room][user_input]
print('Thanks for playing!')
main()
这是对避免dict中出现ErrorKey的代码的改进:
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def main():
current_room = 'Great Hall'
while True:
print('You are in the', current_room + '.')
user_input = input('Where would you like to go?: ')
if user_input == "Quit":
break
possible_directions = rooms[current_room].keys()
if user_input not in possible_directions:
print()
print('You can not go:', user_input)
print('Try instead', )
print(possible_directions)
print()
continue
current_room = rooms[current_room][user_input]
print('Thanks for playing!')
main()
需要注意的是,程序会启动一个无止境的循环。因此,如果您像我在Jupyter笔记本中一样运行它,并且在键入“退出”之前再次运行该单元格,Jupyter仍将同时执行上一个进程和新进程。您的rooms变量不包含退出信息rooms[current_room]['Quit']
while
while循环运行,直到用户输入Quit
。但是,如果是Quit
,代码将尝试访问不存在的房间[当前房间]['Quit']
。您只需执行while True
循环,然后中断
如果用户的输入是退出
,并且只有在您检查之后,才可以运行current\u room=rooms[current\u room][user\u input]
。但您可能需要检查用户输入是否为房间中的有效密钥
:
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
def main():
current_room = 'Great Hall'
while True:
print('You are in the', current_room + '.')
user_input = input('Where would you like to go?: ')
if user_input == "Quit":
break
possible_directions = rooms[current_room].keys()
if user_input not in possible_directions:
print()
print('You can not go:', user_input)
print('Try instead', )
print(possible_directions)
print()
continue
current_room = rooms[current_room][user_input]
print('Thanks for playing!')
main()