2016-02-12 47 views
1

我沿着一個在線程序來自學自己編程,我面臨的問題涉及使用變量的值(在這種情況下爲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 
+0

我冒昧,以縮短你的問題相當多,因爲它會更容易閱讀/理解,如果你單刀直入。另外,你應該總是添加主語言標記,在你的情況'python'中。 – sloth

+0

另外,如果你的代碼已經工作,你想在possbile改進方面進行一些輸入,還有https://codereview.stackexchange.com/ – sloth

回答

1

如果你closly看您的代碼

... 
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)?會發生什麼情況),但這只是一個練習。

0

我認爲最好的答案是懶惰,但我想建議您可以使用名爲Room的類使用public 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() 

更簡潔的代碼和清晰。如果你從一個文件加載所有東西,它也會很棒。每行一個房間。

只是一個建議

相關問題