2011-11-20 142 views
1

不久前我决定采取文本冒险游戏。我一直想这样做。但它第一次做了史诗般的失败。这一次我越来越近,但不在那里。不过,我认为我看到了这个错误:问题是这个变量没有进入下一个def。所以我想知道的是我该如何解决它?python变量传递问题

这是一个能说明问题的一段代码:

def start(): 
    print "Hello there Knight... Knight? Sir Knight, what's your name?" 
    name = raw_input("> ") 
    print "Well sir %s of simpleton. We have a message from the queen, do you want to read it?" % name 
    rm = raw_input("> ") 
    rm(rm) 

def rm(rm): 
    if rm == "yes": 
     print "It says: \n Dear %s, \n Our kingdom is in great danger. Dragon Redpole has captured the beatiful princess. Who ever saves her rom his dangerous castle may marry her." % name 
     print "What will you do? Go undercover to The Far Lands or in full weaponry" 
     UorW = raw_input("type u or fw \n > ") 
    elif rm == "no": 
     print "I am sorry sir, but the queen's word is la.. SHUT UP YOU USELESS PIECE OF TRASH OUT OF THIS ROOM NOW!! You say highly irritated. Will you tell the torturer to torture the butler in the dungeons?" 
     torture_butler = raw_input("> ")  
     torture_butler() 
    else: 
     print "That's not possible" 

这是报告中,我得到:

Traceback (most recent call last): 
    File "story.py", line 59, in <module> 
    start() 
    File "story.py", line 6, in start 
    rm(rm) 
TypeError: 'str' object is not callable 
+3

请修复缩进,此代码不会像发布一样运行。 – NullUserException

回答

6

您覆写名为rm()raw_input("> ")返回值的函数。在此行之后,名称rm将指向一个字符串对象,并且尝试调用此字符串对象失败,因为字符串对象不可调用。重命名变量,使其不会影响函数名称。

+0

谢谢,这一个真的会帮助我! –

1

从你的代码编写方式,姓名rm指的是名称的功能无论是在start功能,也不是rm功能。在这两种情况下,rm是隐藏函数定义的局部变量。

正如其他答案中已经提出的那样,您需要避免重载多重含义相同的名称。