2011-11-20 90 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是隱藏函數定義的局部變量。

正如其他答案中已經提出的那樣,您需要避免重載多重含義相同的名稱。