2013-10-29 47 views
-1

我想從列表下面的死()函數訪問兩個人的名字,並顯示它們。它僅在終端中顯示%s和%s。我已閱讀了Python列表中的文檔,但我看不到我做錯了什麼。如何從一個訪問列表名稱,並顯示他們

from sys import exit 

name = ["Max", "Quinn", "Carrie"] 

def start(): 
    print """ 
    There are a bunch of people beating at the door trying to get in. 
    You're waking up and a gun is at the table. 
    You are thinking about shooting the resistance or escape through out the window. 
    What do you do, shoot or escape? 
    """ 
    choice = raw_input("> ") 

    if choice == "shoot": 
     dead("You manage to get two of them killed, %s and %s, but you die as well.") % (name[1], name[2]) 

這是我的我的死()函數的代碼:

def dead(why): 
    print why, "Play the game again, yes or no?" 

    playagain = raw_input() 

    if playagain == "yes": 
     start() 
    elif playagain == "no": 
     print "Thank you for playing Marcus game!" 
    else: 
     print "I didn't get that, but thank you for playing!" 
    exit(0) 

回答

4

你的括號關閉dead()函數調用應移到該行的末尾。否則,字符串插值發生在其返回值而不是其輸入上。

此致:

dead("You manage to get two of them killed, %s and %s, but you die as well.") % (name[1], name[2]) 

固定:

dead("You manage to get two of them killed, %s and %s, but you die as well." % (name[1], name[2])) 
+0

太謝謝你了約翰。很棒! – lol5433

0

的格式必須是在括號內。原樣,格式將適用於從dead()返回。

dead("You manage to get two of them killed, %s and %s, but you die as well." % (name[1], name[2])) 
相關問題