python – :TypeError:’function’类型的参数不可迭代“,但在
发布时间:2020-12-20 11:56:34 所属栏目:Python 来源:网络整理
导读:我是 Python 2的新手.我正在尝试构建一个“购物车”Python程序,但坚持“在购买之前检查库存”步骤. 首先,我在这里阅读了很多相同问题的线索,但它们似乎与我的不同. 其次,我已经将每个函数分开并在另一个文件中进行测试.他们个人工作得很好.但是当连接check_s
我是
Python 2的新手.我正在尝试构建一个“购物车”Python程序,但坚持“在购买之前检查库存”步骤.
首先,我在这里阅读了很多相同问题的线索,但它们似乎与我的不同. 其次,我已经将每个函数分开并在另一个文件中进行测试.他们个人工作得很好.但是当连接check_stock(y)函数时,它给出了错误. 我相信这个问题来自于“in”命令. 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) 以下是代码的其余部分,如果有帮助的话: 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() 解决方法
您创建了一个名为list的列表(您不应该这样做,因为它已经是内置名称),但是您还创建了一个名为list的函数(再次,不要这样做). list是指现在的功能,而不是你的列表. 因此,当您在列表中检查y时,它会尝试检查项目是否在函数中.你不能在函数上使用,因此错误.解决方案很简单:使用更清晰的名称!
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |