Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何基于另一个变量&x27;从列表中调用元素;s值多少?_Python_List_Python 2.7_Variables - Fatal编程技术网

Python 如何基于另一个变量&x27;从列表中调用元素;s值多少?

Python 如何基于另一个变量&x27;从列表中调用元素;s值多少?,python,list,python-2.7,variables,Python,List,Python 2.7,Variables,我正在跟随一个在线程序自学编程,我面临的问题是使用变量的值(在本例中为当前房间)从已建立的列表(在本例中为房间列表)访问第一个元素 如何根据列表外变量(当前房间)的值(将发生变化)访问列表(房间)中的元素 我根据current\u room的值为room\u list设置单独的输出,从而强制我的代码工作,但是我觉得有更简洁的方法来实现我想要的结果 代码: print("Welcome to Dungeon Escape!\n") #Room List (17 Rooms in total) r

我正在跟随一个在线程序自学编程,我面临的问题是使用变量的值(在本例中为
当前房间
)从已建立的列表(在本例中为
房间列表
)访问第一个元素

如何根据列表外变量(
当前房间
)的值(将发生变化)访问列表(
房间
)中的元素

我根据
current\u room
的值为
room\u 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\u list
中获取子列表所用的索引始终与
current\u room
相同,因此您只需使用

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

当然,您必须添加边界检查(例如,如果
current\u room>=len(room\u list)
?),但这是留给您的一个练习。

我认为最好的答案是sloth提供的,但我想建议您可以使用名为room的类,并使用公共print\u room()方法和get\u 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()
代码更紧凑,也更清晰。如果你从一个文件中加载所有内容,那也太好了。每条线路一个房间


只是一个建议

我冒昧地把你的问题缩短了不少,因为如果你直截了当地说,你的问题会更容易理解。此外,您应该始终添加main language标记,在您的示例中是
python
。此外,如果您的代码已经可以运行,并且您需要一些关于可能的改进的输入,那么还有