在Python中构建基于文本的游戏,区域未定义



我正在python中开发一款基于文本的游戏,除了玩家将前往并收集物品的区域外,其他一切似乎都在运行。当我开始游戏时,它会浏览介绍(我省略了介绍(,然后向我显示此错误以及玩家的统计数据:

item=玩家(位置(在此输入代码第59行,在播放器中返回区域[位置][项目]名称错误:名称"区域"未定义

我真的不知道如何修复它,我一直在尝试定义区域,但我仍然会遇到同样的错误。

def rules():
print(' n Using the commands, North, South, East and West to navigate through the warehouse.')
print('Find your Tiramisu cake! Search the rooms of the warehouse to get your cake!')
print('........Or use Exit to leave the game.')

area = {
'Warehouse': {
'South': 'Basement', 'North': 'Storage Room', 'East': 'Parking Lot', 'West': 'Dusty Room'
},
'Parking Lot': {'West': 'Warehouse','item': 'key'},
'Basement': {'North': 'Warehouse', 'item': 'Kingpin'},
'Storage Room': {'South': 'Warehouse', 'item' : 'Tiramisu Cake'},
'Dusty Room': {'East': 'Warehouse', 'item': 'Safe' } 
}
location = 'Warehouse'
def fetch(location, new_dir):
new_room = location 
for i in area:
if i == location:
if new_dir in area [i]:
new_room = area [i][new_dir]
return new_room
def stats(location):
return area[location]['item']
rules()
pockets = []
#loop starts here
while 1: 
print('n***********************************************')
print('You are in the', location)
print('In your pockets you hold', pockets)
print('***********************************************')
item = area[location].get('item')
print('In this room you see', 'item')
if item == 'Kingpin':
print ('Oh no! The Kingpin!!......He pummels you, Game Over')
exit(0)
if item is not None and new_dir == 'You got ' + item:
if item in pockets:
print('This room seems empty.....oh wait you already got that item')
else:
pockets.append
new_dir = input('n Which direction are you going?: ')
new_dir = new_dir.capitalize
if (new_dir == 'Exit'):
print('Thanks for playing!')
exit(0)
if new_dir == 'North' or new_dir == 'South' or new_dir == 'East' or new_dir == 'West':
new_room = fetch(location, new_dir)
if new_room == location:
print(' n Invalid move, try another direction.')
else:
location = new_room
if new_dir ==str('get'+ 'item'):
if 'item' in pockets:
print('You found', 'item', '!')
pockets.append(area[location]['item'])
else:
print('Invalid move')
if len(pockets) ==3:
print('You have got what you came for, you win! Thanks for playing!')
exit(0)
break

您尚未将area传递给fecth()stats()函数。要么通过,要么使其成为全球

最新更新