我一直在處理以下代碼,但似乎無法讓我的專案從字典移動到庫存串列。我一定在某個地方有錯誤,但我很難找到它。有人可以幫我嗎?我認為我的問題是它有:
if command == "get item": if 'item' in rooms: inventory.append(rooms[current_room]['item']) del rooms[current_room]['item'] print ('Items in Inventory:. {}.'.format(inventory)) 我是 Python 新手,只需要一點點幫助來了解它的格式化方式。提前致謝。
'Directions: To move around the ship: type a go command in Nautical Terms such as "go forward, go aft,'
' go starboard, or go port". To put an item into your inventory: type "get *name of item*" Remember, '
'you will need to collect all six items before you get to the shark or you will be shark bait.\n'
'If you would like to end the game, type exit.\n\n')
rooms = {'Hull': {'name': 'the Hull', 'go forward': 'Bow', 'go aft': 'Stern',
'go starboard': 'Cabin B', 'go port': 'Galley', 'item': ' ', 'item name': ' ',
'text': 'You are in the Hull.'}, #starting point, no items
'Bow': {'name': 'the Bow', 'go port': 'Bridge', 'go aft': 'Hull', 'item': 'Chum Bucket',
'item name': 'a Chum Bucket',
'text': 'You are in the Bow.'},
'Bridge': {'name': 'the Bridge', 'go starboard': 'Bow', 'item': 'Oxygen Tank',
'item name': 'an Oxygen Tank',
'text': 'You are in the Bridge.'},
'Stern': {'name': 'the Stern', 'go forward': 'Hull', 'go port': 'Cabin A', 'item': 'Rope',
'item name': 'a Rope',
'text': 'You are in the Stern'},
'Cabin A': {'name': 'Cabin A', 'go starboard': 'Stern', 'item': 'Spearhead',
'item name': 'a Spearhead',
'text': 'You are in Cabin A'},
'Cabin B': {'name': 'Cabin B', 'go port': 'Hull', 'item': 'Spear Gun', 'item name': 'a Spear Gun',
'text': 'You are in Cabin B'},
'Galley': {'name': 'the Galley', 'go starboard': 'Hull', 'go aft': 'Lido Deck', 'item': 'Dive Knife',
'item name': 'a Dive Knife',
'text': 'You are in the Galley'},
'Lido Deck': {'name': 'the Lido Deck', 'go forward': 'Galley', 'item': 'Shark', #villan
'item name': 'A SHARK!',
'text': 'You are at the Lido Deck'},
}
directions = ['go aft', 'go forward', 'go port', 'go starboard']
get_items = ['get item']
current_room = rooms['Hull']
inventory = []
while True:
if current_room['name'] == 'the Lido Deck':
print('You are now on the Lido Deck and you see a SHARK!!!')
print('Congratulations! You have killed the shark! Way to go!')
print('You are in {}.'.format(current_room['name']), '\n', 'You have {}'.format(inventory), '\n',
'You see {}.'.format(current_room['item name']))
command = input('\nWhat would you like to do?')
if command in directions:
if command in current_room:
current_room = rooms[current_room[command]]
else:
print('You have hit a wall. Try another direction.')
if command == "get item":
if 'item' in rooms:
inventory.append(rooms[current_room]['item'])
del rooms[current_room]['item']
print('Items in Inventory:. {}.'.format(inventory))
uj5u.com熱心網友回復:
inventory.append(rooms[current_room]['item name'])
# not
inventory.append(rooms[current_room]['item'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322312.html
