gpt4 book ai didi

python - 如何根据另一个变量的值调用列表中的元素?

转载 作者:太空宇宙 更新时间:2023-11-03 16:56:56 33 4
gpt4 key购买 nike

我正在遵循一个在线程序来自学编程,我面临的问题涉及使用变量的值(在本例中为current_room)来访问第一个元素,来自已建立的列表(在本例中为room_list。)

如何根据列表外部变量 (current_room) 的值(将更改)来访问列表 (room_list) 中的元素?

我设法通过根据 current_room 的值设置 room_list 的单独输出来强制我的代码工作,但是我觉得有更简洁的方法来实现我的期望结果。

代码:

print("Welcome to Dungeon Escape!\n")

#Room List (17 Rooms in total)
room_list = []
#0
room = ['''You are in Your Cell.
The door to the East is open.\n''', None,1,None,None]
room_list.append(room)
#1
room = ['''You are in a Hallway.
The Hallway extends North and South.
A Door is to the East.
Your Cell is to the West\n''',2,6,4,0]
room_list.append(room)
#2
room = ['''You walked to the North end of the Hallway.
A Cell door is to the West.
The Hallway extends South.\n''',None,None,31,1]
room_list.append(room)
#3
room = ['''You entered the Cell.
A rotting corpse is chained to the wall.
The Cell door is to the East.\n''',None,2,None,None]
room_list.append(room)
#4
room = ['''You walked to the South end of the Hallway.
A Cell door is to the West.
The Hallway extends North.\n''',1,None,None,5]
room_list.append(room)
#5
room = ['''You entered the Cell.
Rats scurry into the walls.
The Cell door is to the East.\n''',None,4,None,None]
room_list.append(room)
#6
room = ['''You are in a long passage.
Torches light the way ahead.
Doors lead North, South, and East.\n''',7,None,9,1]
room_list.append(room)
#7
room = ['''You entered the Guard's Office.
There are signs of a struggle.
Doors lead to the South and West.\n''',None,None,6,8]
room_list.append(room)
#8
room = ['''You force your way into the Armory.
No supplies remain inside.
The door is to the East.\n''',None,7,None,None]
room_list.append(room)
#9
room = ['''You entered the stairwell.
A long flight of stairs is before you.
Proceed South to climb up or North to climb down.\n''',6,None,10,None]
room_list.append(room)
#10
room = ['''The Throne Room is atop the stairs.
Rotting food from a great feast fills your nostrils.
There are paths to the North and South.\n''',9,None,11,None]
room_list.append(room)
#11
room = ['''You are in the kitchen.
A fire has not been lit here in a while.
Doors lead to the North and West.\n''',10,None,None,12]
room_list.append(room)
#12
room = ['''You walk into the pantry.
It appears to be ransacked.
Doors lead East and South.\n''',None,11,13,None]
room_list.append(room)
#13
room = ['''You step into the Courtyard.
Finally there is fresh air.
Paths lead to North, East, South, and West.\n''',12,14,15,16]
room_list.append(room)
#14
room = ['''You take the path East.
The wall at the end is too tall to climb.
The return path leads West.\n''',None,None,None,13]
room_list.append(room)
#15
room = ['''You take the path South.
The Gate is blocked by burning debris.
The return path leads North.\n''',13,None,None,None]
room_list.append(room)
#16
room = ['''You take the path West.
A break in the wall leads further West.
The return path leads East.\n''',None,13,None,17]
room_list.append(room)
#17
room = ['''You escape throught he break in the wall.
Congratulations you are free!
Thanks for playing Dungeon Escape!\n''',None,None,None,None]
room_list.append(room)

#Variables
current_room = 0
done = False

while not done:
if current_room == 0:
print room_list[0][0]
elif current_room == 1:
print room_list[1][0]
elif current_room == 2:
print room_list[2][0]
elif current_room == 3:
print room_list[3][0]
elif current_room == 4:
print room_list[4][0]
elif current_room == 5:
print room_list[5][0]
elif current_room == 6:
print room_list[6][0]
elif current_room == 7:
print room_list[7][0]
#elif statements continue up to 17
else:
done = True

最佳答案

如果你仔细查看你的代码

...
if current_room == 0:
print room_list[0][0]
elif current_room == 1:
print room_list[1][0]
elif current_room == 2:
print room_list[2][0]
...

您应该在这里看到一个模式:

用于从 room_list 获取子列表的索引始终与 current_room 相同,因此您只需使用

...
print room_list[current_room][0]
...

当然,您必须添加边界检查(例如,如果 current_room >= len(room_list) 会发生什么?),但这留给您作为练习。

关于python - 如何根据另一个变量的值调用列表中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354811/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com