我怎样才能打印哪些房间连接到" de Lobby"?我尝试的东西返回字符串错误或其他错误。
kamers = {
1 : { "naam" : "de Lobby" ,
"trap" : 2,
"gangrechtdoor" : 3 } ,
2 : { "naam" : "de Trap" ,
"lobby" : 1,
"note" : "Terwijl je de trap oploopt hoor je in de verte Henk van Ommen schreeuwen" } ,
3 : { "naam" : "de Gang rechtdoor" ,
"lobby" : 1,
"gymzaal" : 4,
"concergie" : 5,
"gangaula" : 6 } ,
这会打印您所在的位置,但正如您所看到的,不是连接的是哪个房间。
print("Hier ben je: " + kamers[currentKamer]["naam"])
print("hier kan je naartoe: ")
答案 0 :(得分:0)
这样做你想要的吗?
kamers = {
1: {"naam": "de Lobby",
"trap": 2,
"gangrechtdoor": 3},
2: {"naam": "de Trap",
"lobby": 1,
"note": "Terwijl je de trap oploopt hoor je in de verte Henk van Ommen schreeuwen"},
3: {"naam": "de Gang rechtdoor",
"lobby": 1,
"gymzaal": 4,
"concergie": 5,
"gangaula": 6}}
def find_connected_rooms(room_name, rooms):
room_number = next(room_number for room_number, props in rooms.items() if props['naam'] == room_name)
for room_props in rooms.values():
if room_number in room_props.values():
yield room_props['naam']
if __name__ == '__main__':
for connected_room in find_connected_rooms('de Lobby', kamers):
print(connected_room)
<强>输出强>
de Trap
de Gang rechtdoor
答案 1 :(得分:0)
问题不是很清楚,但我假设您正在寻找具有大厅钥匙或任何带有1值的钥匙的物品
kamers [1]是大厅,它是&#34; naam&#34;是&#34; de Lobby&#34;。
所以,如果里面的项目的值为1(Lobby&#39; s键)
,则会得到[i for i in kamers.values() if 1 in i.values()]
或者您可以检查密钥&#39; lobby&#39;存在
[i for i in kamers.values() if i.get('lobby',None) ]
获取可替换的房间名称&#34; i&#34;与我[&#39; naam&#39;]
[i['naam'] for i in kamers.values() if i.get('lobby',None) ]
返回
['de Trap', 'de Gang rechtdoor']