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

时间:2016-02-12 04:25:00

标签: python list python-2.7 variables

我跟随在线程序自我编程自己编程和我面临的问题涉及使用变量的值(在这种情况下为current_room)来访问第一个元素,从已建立的列表(在这种情况下为room_list。)

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

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

代码:

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

2 个答案:

答案 0 :(得分:1)

如果您仔细查看代码

...
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)会发生什么?),但这是留给你的练习。

答案 1 :(得分:0)

我认为最好的答案是来自懒惰的答案,但我想建议您可以使用名为Room的类使用公共print_room()方法和get_index()方法,这样您就可以:

room = Room('''You are in Your Cell.The door to the East is open.\n''', None,1,None,None)
room_list.append(room)

room = 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)

# ... keeps up until 17 rooms...

current_room = 0 # or whatever

# This now remains 3 lines
for room in room_list:
    if current_room == room.get_index():
        room.print_room()

更紧凑的代码也清晰易读。如果您从文件加载所有内容,它也会很棒。每行一个房间。

只是一个建议