需要帮助定义基于文本的游戏的功能



我正在为这个项目编写一个函数。我是python的新手,正在网上学习。我正在尝试定义一个函数,该函数可以读取我的玩家所在的房间,并给出与之相关的谜题。到目前为止,这就是我所拥有的,current_room还可以使用另一本字典。我只是在尝试测试运行时不断出现关键错误。如果还有更好的方法,请告诉我。

编辑:使用完整代码更新

# The dictionary links a room to other rooms.
rooms = {'Warehouse Storage': {'name': 'Warehouse Storage', 'south': 'Stamping Area',
'text': 'You are in the Warehouse Storage.'},
'Machine Shop': {'name': 'Machine Shop', 'east': 'Scrap Pile', 'south': 'Staging Area',
'text': 'You are in the Machine Shop.'},
'Front Offices': {'name': 'Front Offices', 'west': 'Lunchroom',
'text': 'You are in Front Offices.'},
'Stamping Area': {'name': 'Stamping Area', 'west': 'Staging Area', 'north': 'Warehouse Storage',
'text': 'You are in the Stamping Area'},
'Loading Docks': {'name': 'Loading Docks', 'east': 'Staging Area',
'text': 'You are in the Loading Docks'},
'Staging Area': {'name': 'Staging Area', 'east': 'Stamping Area', 'west': 'Loading Docks',
'south': 'Lunchroom', 'north': 'Machine Shop',
'text': 'You are in the Staging Area'},
'Lunchroom': {'name': 'Lunchroom', 'north': 'Staging Area', 'east': 'Front Offices',
'text': 'You are in the Lunchroom'},
'Scrap Pile': {'name': 'Scrap Pile', 'west': 'Machine Shop',
'text': 'You are in the Scrap Pile'}
}
riddles = {'Warehouse Storage': {'name': 'Warehouse Storage', 'question': '', 'answer': ''},
'Machine Shop': {'name': 'Machine Shop', 'question': '', 'answer': ''},
'Stamping Area': {'name': 'Stamping Area', 'question': '', 'answer': ''},
'Staging Area': {'name': 'Staging Area', 'question': '', 'answer': ''},
'Lunchroom': {'name': 'Lunchroom', 'question': 'riddle', 'answer': 'yes'},
'Scrap Pile': {'name': 'Scrap Pile', 'question': '', 'answer': ''}
}

# define function that reads room and prompts user with riddle
def room_puzzle(current_room):
global user_inventory
if current_room in riddles.keys():
riddle = input(f'{riddles[current_room]["question"]} n').strip()
if riddle == riddles[current_room]['answer']:
user_inventory += 1
print('You gained a key')
else:
print('that is incorrect')
return user_inventory
else:
print("no such room")
# determine command regardless of capitalization
def dir_input(text):
if text.upper().startswith('W'):
return 'west'
elif text.upper().startswith('N'):
return 'north'
elif text.upper().startswith('S'):
return 'south'
elif text.upper().startswith('E'):
return 'east'
elif text.upper().startswith('Q'):
return 'quit'

directions = ['north', 'south', 'east', 'west']
current_room = rooms['Front Offices']
print('Would you like to play a game?')
start = input()
game_start = False
user_inventory = 0 # start at zero
if start == 'yes' or start == 'Yes':
game_start = True
# game loop
while game_start == True:
# win condition
if current_room['name'] == 'Loading Docks':
if user_inventory == 6:
print(current_room['text'])
else:
print('6 keys are required to enter')
# display current location
print()
print(current_room['text'])
# get user input
command = input('nWhat do you do? ').strip()
# show inventory
if command == 'show inventory':
print('You have {} keys'.format(user_inventory))
# movement
elif command in directions:
command = dir_input(command)
if command in current_room:
current_room = rooms[current_room[command]]
#room_puzzle()
else:
# not a valid input
print("You can't go that way.")
# quit game
elif command == 'quit':
break
# does not compute
else:
print("I don't understand that command.")

基本上,我从上面的代码中观察到的是,你想检查current_room是否等于Lunchroom,或者你的谜语词典中是否有其他房间。因此,您必须检查字典中是否存在关键字,而不是值。此外,以下部分在您的代码中是不正确的if current_room in riddles['name']:

相反,它应该是

if current_room in riddles.keys():

并且代码if riddle == riddles['answer']:的部分不正确,因为您没有将正确的引用传递给字典密钥

应该是if riddle == riddles[current_room]['answer']:

因此,以下是正确的代码和工作演示,供您理解。

riddles = {'Lunchroom': {'name': 'Lunchroom', 'question': 'riddle?', 'answer': 'yes'}}
def room_puzzle(current_room):
global user_inventory
if current_room in riddles.keys():
riddle = input('{}'.format(riddles[current_room]['question'])).strip()
if riddle == riddles[current_room]['answer']: # here you have to define the refrence to the dictionary value, using the parent key first
# user_inventory += 1
print('You gained a key')
else:
print('that is incorrect')
# return user_inventory
else:
print("no such room")
current_room = 'Lunchroom' #let's assume for now
room_puzzle(current_room)
current_room = 'Someroom' #let's now change the current_room and check that
room_puzzle(current_room)

最新更新