简体   繁体   中英

:TypeError: argument of type 'function' is not iterable", but works when individually tested

I am a newbie to Python 2. I am trying to build a "shopping cart" Python program, but stuck at "check stock before put to cart" step.

Firstly, I have read many threads of the same problems in here, but they are seemingly of different cause from mine.

Secondly, I already separated each function and test in a different file. They worked well individually. But when joined back the check_stock(y) function, it gave the error.

I believe that the problem comes from the "in" command.

    def check_stock(y):             #// Problem in this function // 
        if y in list:
            print "%s is available" % y
            add_to_cart(y)
        else:
            print "Sorry, but %s is not available." % y

    def check_finish():
        y = raw_input(">")
        if y == "checkcart":        
            print cart              #check inside shopping cart
        elif y == " ":
            check_finish()          #loop back for blank
        elif y == "list":
            list()                  #present the list
        else:
            while y != "ok":        #"ok" = finished shopping
                check_stock(y)
            else:
                print "Checking out..."
                sorted(cart)
                print "Your item(s) are %s." % cart
                exit(0)

Here are the rest of the code, if it may help:

cart = []
list = ['apple', 'banana', 'cat', 'dog', 'elephant', 'flamingo', 'goofy', 'ham']
a = 0

def list():
    print list                  #present the list
def representInt(s):            #check if value is integer
    try:
        int(s)
        return True
    except ValueError:
        return False
def annoyedAtError(a):          #interaction for repeated mistakes
    if a < 2:
        print "Numbers only please"
    elif 2 < a < 4:
        print "Man, just do as I say, please. I have another shift tonight."
    elif a == 5 :
        print "Hey, seriously?"
    else:
        print "..."
def check_stock(y):             #// PROBLEM HERE // cross-check with list if item is available
    if y in list:
        print "%s is available" % y
        add_to_cart(y)
    else:
        print "Sorry, but %s is not available." % y

def add_to_cart(y):
    amount = (raw_input("How many do you want to add? > "))
    if representInt(amount) == False:
        annoyedAtError(a)
        global a 
        a = a + 1
        add_to_cart(y)
    else: 
        y = y + " " + amount
        print "%s is added to cart" % (y)
        cart.append(y)
        check_finish()
def check_finish():
    y = raw_input(">")
    if y == "checkcart":        
        print cart              #check inside shopping cart
    elif y == " ":
        check_finish()          #loop back for blank
    elif y == "list":
        list()                  #present the list
    else:
        while y != "ok":        #"ok" = finished shopping
            check_stock(y)
        else:
            print "Checking out..."
            sorted(cart)
            print "Your item(s) are %s." % cart
            exit(0)
def welcome():      
    print """\nWelcome to cyber shopping.\n 
    Please enter things you want to buy.
    Check your cart by typing: checkcart
    type "ok" when finished.
    type "list" for things available for buying"""
def start():
    welcome()
    check_finish()

start()

You created a list named list (which you shouldn't do, because it's a built in name already), but then you also create a function named list (again, don't do this). list refers to the function now, not your list. So when you check y in list it tries to check if the item is in the function. You can't use in on a function, hence the error. The solution is simple: use clearer names for things!!

lst = ['apple', 'banana', 'cat', 'dog', 'elephant', 'flamingo', 'goofy', 'ham']
a = 0

list is a predefined function in python, so use some other name.

First, don't name a list list ! Second, the more descriptive the names of your function, the better. So here's some code with the names edited:

cart = []
groceries = ['apple', 'banana', 'cat', 'dog', 'elephant', 'flamingo', 'goofy', 'ham']
a = 0

def prntgroceries():
    print groceries                  #present the groceries
def representInt(s):            #check if value is integer
    try:
        int(s)
        return True
    except ValueError:
        return False
def annoyedAtError(a):          #interaction for repeated mistakes
    if a < 2:
        print "Numbers only please"
    elif 2 < a < 4:
        print "Man, just do as I say, please. I have another shift tonight."
    elif a == 5 :
        print "Hey, seriously?"
    else:
        print "..."
def check_stock(y):             #// PROBLEM HERE // cross-check with list if item is available
    if y in groceries:
        print "%s is available" % y
        add_to_cart(y)
    else:
        print "Sorry, but %s is not available." % y

def add_to_cart(y):
    amount = (raw_input("How many do you want to add? > "))
    if representInt(amount) == False:
        annoyedAtError(a)
        global a 
        a = a + 1
        add_to_cart(y)
    else: 
        y = y + " " + amount
        print "%s is added to cart" % (y)
        cart.append(y)
        check_finish()
def check_finish():
    y = raw_input(">")
    if y == "checkcart":        
        print cart              #check inside shopping cart
    elif y == " ":
        check_finish()          #loop back for blank
    elif y == "list":
        prntgroceries()                  #present the list of groceries
    else:
        while y != "ok":        #"ok" = finished shopping
            check_stock(y)
        else:
            print "Checking out..."
            sorted(cart)
            print "Your item(s) are %s." % cart
            exit(0)
def welcome():      
    print """\nWelcome to cyber shopping.\n 
    Please enter things you want to buy.
    Check your cart by typing: checkcart
    type "ok" when finished.
    type "list" for things available for buying"""
def start():
    welcome()
    check_finish()

start()

Second thing of note, unrelated to your question: when you ask for the things you can buy, it prints the list...and ends the program. I would suggest doing something to prevent the program from ending right after you print the list of things available. Maybe even print that list at the very beginning.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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