Python变量传递问题

2024-09-29 23:17:33 发布

您现在位置:Python中文网/ 问答频道 /正文

不久前我决定参加一个文字冒险游戏。我一直想这么做。但它第一次以史诗般的失败告终。这一次我越来越近了,只是不在那里。不过,我想我看到了错误:问题是变量没有被带到下一个def。所以我想知道的是我该怎么解决它?你知道吗

这是一段描述问题的代码:

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

Tags: thetormnameinyouinputraw
2条回答

用返回值raw_input("> ")覆盖名为rm()的函数。在这一行之后,名称rm将指向一个string对象,尝试调用这个string对象失败,因为string对象是不可调用的。重命名变量,使其不影响函数名。你知道吗

从编写代码的方式来看,名称rmstart函数和rm函数中都没有引用该名称的函数。在这两种情况下,rm都是隐藏函数定义的局部变量。你知道吗

正如在其他答案中已经提出的那样,您需要避免用多个含义重载同一个名称。你知道吗

相关问题 更多 >

    热门问题