[英]Building a Text Based Game in Python, area not defined
我正在 python 中開發基於文本的游戲,除了玩家將前往並收集物品的區域外,一切似乎都在工作。當我開始游戲時,它會通過介紹(我忽略了)和然后向我顯示此錯誤以及玩家的統計信息:
item = player(location) enter code hereline 59, in player return area[location]['item'] NameError: name 'area' is not defined
我真的不明白如何解決它,我一直在嘗試定義區域,但我仍然遇到同樣的錯誤。
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()
函數。 要么通過它,要么讓它全球化
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.