所以我是個白癡,幾乎在 main 函式之外撰寫了 mina 代碼。我仍然是新手并且正在學習,所以當我將代碼移入主函式時,我無法訪問已定義的變數。我的位置和庫存,因為它們是在主函式中定義的,所以當我嘗試呼叫 status() 時無法正常作業。但是當我將庫存和位置移到主要功能之外時,庫存無法進入房間。任何人都可以指出我解決這個問題的正確方向。謝謝
def instructions():
print("\n Super Humans Adventure Game")
print("-------------Instructions-------------")
print("Collect the 6 items within the rooms to")
print("defeat the scientist and win.")
print("To move type: North, South, West, or East")
print('-' * 38)
def status():
print('-' * 20)
print('You are in the {}'.format(location['name']))
print('Your current inventory: {}\n'.format(inventory))
if location['item']:
print('Item in room: {}'.format(', '.join(location['item'])))
print('')
#calls instructions
instructions()
def main():
rooms = {
'Main Entrance': {
'name': 'Main Entrance',
'item': [],
'East': 'Dining Room'},
'Dining Room': {
'name': 'Dining Room',
'item': ['potion'],
'West': 'Main Entrance',
'North': 'Laboratory',
'East': 'Break Room',
'South': 'Holding Cells'},
'Laboratory': {
'name': 'Laboratory',
'item': ['shield'],
'East': 'Office',
'South': 'Dining Room'},
'Office': {
'name': 'Office',
'item': [],
'West': 'Laboratory'}, # Villian
'Break Room': {
'name': 'Break Room',
'item': ['key'],
'West': 'Dining Room',
'East': 'Bathroom'},
'Bathroom': {
'name': 'Bathroom',
'item': ['suit'],
'West': 'Break Room'},
'Holding Cells': {
'name': 'Holding Cells',
'item': [],
'East': 'Armory',
'North': 'Dining Room'},
'Armory': {
'name': 'Armory',
'item': ['weapon'],
'North': 'Power Room',
'West': 'Holding Cells'},
'Power Room': {
'name': 'Power Room',
'item': ['power'],
'South': 'Armory'}
}
location = rooms['Holding Cells']
directions = ['North', 'East', 'South', 'West']
inventory = []
while True:
if location == rooms['Office'] and len(inventory) > 5:
print('')
print('You have defeated the scientist and escaped! Congratulations')
elif location == rooms['Office'] and len(inventory) < 6:
print('')
print('You have reached the scientist but you are too weak!')
print('You have died')
break
# shows current location
status()
# user input
cmd = input('Enter move: ').capitalize().strip()
if cmd in directions:
if cmd in location:
location = rooms[location[cmd]]
print('You successfully moved locations.')
else:
print('')
print('You can not go that way!')
# quit game
elif cmd.lower in ('q', 'quit'):
print('You have quit the game, thanks for playing!')
break
# get item
elif cmd.lower().split()[0] == 'get':
item = cmd.lower().split()[1]
if item in location['item']:
location['item'].remove(item)
inventory.append(item)
else:
print('There is no item here.')
else:
print('That is not a valid input')
main()
uj5u.com熱心網友回復:
你可以
- 在 Main 中創建一個名為“倉庫”的物件,每個專案及其庫存都可以在倉庫物件中維護,因此您不需要將其作為子程式,和/或
- 您可以在代碼開始時在 main 或其他子例程之外創建“全域變數”,或者
- 您可以將一個變數傳遞給您的子例程(從 Main),并在子例程完成時將回傳值傳回給 main。
uj5u.com熱心網友回復:
將它們作為引數傳遞實際上是解決方案。有人提到了這一點,這正是我所需要的。這是更新代碼
def status(location, inventory, rooms):
print('-' * 20)
print('You are in the {}'.format(location['name']))
print('Your current inventory: {}\n'.format(inventory))
if location['item']:
print('Item in room: {}'.format(', '.join(location['item'])))
print('')
然后在呼叫函式時我只是填寫了引數。
# shows current location
status(location, inventory, rooms)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/391626.html
