_emphasized text_Here是目前为止的代码:
# This displays the game's basic instructions.
def show_instructions():
print("The Haunted Manor")
print("Collect Pawpaw's Lucky Charms and Break the Ancient Curse")
print("Avoid the cursed items haunting the Manor")
print("Move Commands: 'go north', 'go south', 'go east', 'go west'")
print("Get Items: get 'item name'")
print("Type 'exit' if you'd like to quit\n")
# This defines the player's status, and displays it.
def player_status():
print(current_room['text'])
print()
print("You are in: {}".format(current_room['name']))
print("Items in your possession: {}".format(current_items))
print("You see {}".format(current_room['item']))
# The following is a dictionary of rooms, their connections, items in them, and other information
rooms = {
'Foyer': {
'name': 'Foyer',
'west': 'Dining Room',
'south': 'Great Hall',
'east': 'Billiards Room',
'item': 'nothing',
'text': 'Welcome to the Foyer'
},
# Start Room
'Dining Room': {
'name': 'Dining Room',
'east': 'Foyer',
'south': 'Kitchen',
'item': 'Haunted Spork',
'text': 'You have entered the Dining Room'
},
'Billiards Room': {
'name': 'Billiards Room',
'west': 'Foyer',
'item': 'Lucky Rabbit Foot',
'text': 'You are in the Billiards Room'
},
'Kitchen': {
'name': 'Kitchen',
'north': 'Dining Room',
'east': 'Great Hall',
'south': 'Laundry',
'item': 'Silver Spoon',
'text': 'Welcome to the Kitchen'
},
'Great Hall': {
'name': 'Great Hall',
'north': 'Foyer',
'west': 'Kitchen',
'south': 'Master Suite',
'east': 'Observatory',
'item': 'Monkey Paw',
'text': 'The Great Hall welcomes you'
},
'Observatory': {
'name': 'Observatory',
'west': 'Great Hall',
'south': 'Armory',
'item': 'Ancient Curse',
'text': 'You have entered the Observatory',
},
# Boss Room
'Laundry': {
'name': 'Laundry',
'north': 'Kitchen',
'east': 'Master Suite',
'item': 'Lucky Hat',
'text': 'You have entered the Laundry'
},
'Master Suite': {
'name': 'Master Suite',
'north': 'Great Hall',
'west': 'Laundry',
'south': 'Greenhouse',
'east': 'Armory',
'item': 'Horseshoe Belt Buckle',
'text': 'This is the Master Suite'
},
'Armory': {
'name': 'Armory',
'west': 'Master Suite',
'north': 'Observatory',
'item': 'Shooting Star',
'text': 'Welcome to the Armory'
},
'Greenhouse': {
'name': 'Greenhouse',
'north': 'Master Suite',
'item': 'Four-Leaf Clover',
'text': 'Welcome to the Greenhouse'
},
}
# The following sets the player's initial location and items.
current_room = rooms['Foyer']
current_items = None
player_action = ''
show_instructions()
while True:
player_status()
player_action = input("What do you want to do?\n")
player_action = player_action.split()
if player_action[0] == 'exit':
print("Come Back Soon")
break
elif len(player_action) != 2:
print("That doesn't make any sense\n")
elif player_action[0] == 'go':
if player_action[1] in rooms[current_room]:
current_room = rooms[current_room][player_action[1]]
else:
print("You cannot go that way")
elif player_action[0] == 'get':
if player_action[1] in rooms[current_room]:
current_items += [player_action[1]]
print("You got: ", player_action[1])
del rooms[current_room]['item']
else:
print("What exactly were you looking for?")我正在接受用户输入,将其分解为一个列表,使用第一个条目作为命令player_action[0] (第124和第130行),并试图使用第二个条目player_action[1]作为目标。
但是,当我试图在程序中使用这些命令时,我会得到以下错误:
跟踪(最近一次调用):文件"C:\Users\josep\OneDrive\Desktop\IT 140\ADVENTURE GAME\main.py",第125行,在if player_action1 in roomscurrent_room: TypeError: unhashable type:'dict‘中
我该如何解决这个问题?
发布于 2021-08-26 02:54:13
试一试
if player_action[1] in rooms[current_room['name']]
current_room是一本字典..。你不能用一本字典作为另一本字典的钥匙(当你创建它的时候,你确实不是.)
current_room的一个键是"name",它实际上是您房间字典中的密钥。
将另一个实例更改为
current_room = rooms[current_room][player_action[1]]
至
new_room_name = current_room[player_action[1]]
new_room = rooms[new_room_name]
current_room = new_roomhttps://stackoverflow.com/questions/68931924
复制相似问题