简体   繁体   English

TypeError:“函数”类型的参数不可迭代

[英]TypeError: argument of type 'function' is not iterable

Error: 错误:

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

This is the code block that is getting the error: 这是出现错误的代码块:

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

I think I am getting the error because it is recursive function but I use them through out the game and they worked untill I added try_again(). 我认为我得到了错误,因为它是递归函数,但是我在整个游戏中都使用了它们,直到我添加try_again()为止它们一直起作用。

Here is the rest of the code in case it helps: 如果有帮助,下面是其余代码:

    # 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()

The problem is that in the second line of the body of the function try_again you're not calling uput , you're assigning it to user_input . 问题是在try_again函数主体的第二行中,您没有调用uput ,而是将其分配给user_input You forgot the parenthesis, if you change it to this: 如果将其更改为以下内容,则会忘记括号:

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

it will work. 它会工作。

Reading the traceback: 读取回溯:

TypeError: argument of type 'function' is not iterable

The message is telling you that you tried to do an iteration over user_input , the iteration happened because of the in statement. 该消息告诉您,您尝试对user_input进行迭代,因为in语句,所以发生了迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM