如何从列表中访问名称并显示它们

时间:2013-10-29 20:14:37

标签: python list function

我正在尝试从列表中访问两个名称并在下面的dead()函数中显示它们。它只显示终端中的%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])

这是我的dead()函数的代码:

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)

2 个答案:

答案 0 :(得分:4)

关闭dead()函数调用的parens应该移到行尾。否则,字符串插值将在其返回值而不是其输入上发生。

此致:

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]))

答案 1 :(得分:0)

格式需要在括号内。因此,格式将适用于dead()的返回。

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