2014-09-27 121 views
1

錯誤:類型錯誤:類型的參數「功能」不是可迭代

Traceback (most recent call last): 
    File "ex36.py", line 100, in <module> 
    start() 
    File "ex36.py", line 16, in start 
    giant() 
    File "ex36.py", line 59, in giant 
    dead() 
    File "ex36.py", line 70, in dead 
    try_again()  
    File "ex36.py", line 85, in try_again 
    if "y" in user_input or "yes" in user_input: 
TypeError: argument of type 'function' is not iterable 

這是代碼塊中得到的錯誤:

def try_again(): 
    print "Would you like to try again?" 
    user_input = uput 
    if "y" in user_input or "yes" in user_input: 
     start() 
    elif "n" in user_input or "no" in user_input: 
     sys.exit() 
    else: 
     try_again() 

我想我得到,因爲它的誤差是遞歸函數,但我通過遊戲使用它們,直到我添加了try_again()。

下面是代碼的情況下,其餘它可以幫助:

# Role Playing Game I made to practice my Python skills! 
import sys 

def uput(): # get user input and make it all lowercase 
    return raw_input("> ").lower() 

def start(): #Starts the game 
    print "You are in room with a left and right door." 
    print "Which do you choose?" 
    user_input = uput() 
    if user_input == "left": 
     print "You open the door..." 
     lavapit() 
    elif user_input == "right": 
     print "You open the door..." 
     giant() 
    else: 
     print "Try again." + "\n" * 3 
     start() 

def lavapit(): 
    print "Your on the edge of valcano." 
    mountain(10) 
    print "One wrong step and you fall into the volcano!" 
    print "You can walk along the edge to the other side and take the path down to the base" 
    print "Or return to the room!" 
    user_input = uput() 
    if "walk" in user_input or "along" in user_input or "edge" in user_input: 
     print "You stumble." 
     print "And fall off into the lava!" 
     dead() 
    elif "return" in user_input or "door" in user_input or "go back" in user_input: 
     start() 
    else: 
     print "Try again." + "\n" * 3 
     lavapit() 

def giant(): 
    print "You enter a dark musty room." 
    print "With very high ceilings." 
    print "Then you hear... heavy breathing." 
    print "Do you walk slowly and quitely to your right or left." 

    right = 0 
    left = 0 
    while right <= 4 or left <= 5: 
     user_input = uput() 
     if "right" in user_input and right < 3: 
      print "You edge slowly to the right." 
      right += 1 
      left -= 1 
     elif "left" in user_input and left < 4: 
      print "You edge closer to the left." 
      right -= 1 
      left += 1 
     elif "right" in user_input and right > 2: 
      print "You bump into the giant and get eaten alive." 
      print "The giant says, 'Yum'." 
      dead() 
     elif "left" in user_input and left > 3: 
      print "You reach a dark hole in the wall the size of giant mouse." 
      print "Do you enter it?" 
      hole() 
     else: 
      print "Try again." + "\n" * 3 
      giant() 
def dead(): 
    print "-" * 30 
    print "Sorry you loose!" 
    try_again()  

def hole(): 
    user_input = uput() 
    if "yes" in user_input or "y" in user_input: 
     print "Congratulations! You won nothing!" 
     try_again() 
    elif "no" in user_input or "n" in user_input: 
     return 
    else: 
     hole() 

def try_again(): 
    print "Would you like to try again?" 
    user_input = uput 
    if "y" in user_input or "yes" in user_input: 
     start() 
    elif "n" in user_input or "no" in user_input: 
     sys.exit() 
    else: 
     try_again() 


def mountain(x): 
    i = 2 
    while x > 0: 
     print " " * x + "@" * i 
     x -= 1 
     i += 2 

start() 

回答

1

的問題是,在try_again你不調用uput的函數體的第二行,你給它分配到user_input。你忘了括號,如果你改變它:

def try_again(): 
    print "Would you like to try again?" 
    user_input = uput() 
    if "y" in user_input or "yes" in user_input: 
     start() 
    elif "n" in user_input or "no" in user_input: 
     sys.exit() 
    else: 
     try_again() 

它會工作。

讀回溯:

TypeError: argument of type 'function' is not iterable 

的消息告訴你,你嘗試了user_input做一個迭代,迭代發生,因爲in聲明。

+0

謝謝。現在我感到很蠢,那是一個非常簡單的錯誤。感謝您的有用的職位。你的男人拉斐爾! – Grouchy 2014-09-27 15:45:12

+0

@ user3583194嘿,簡單的錯誤發生!這完全是關於閱讀有時不明顯的回溯。 – 2014-09-27 22:54:11

相關問題