
首先,我在这里阅读了很多相同问题的线索,但它们似乎与我的不同.
其次,我已经将每个函数分开并在另一个文件中进行测试.他们个人工作得很好.但是当连接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 = 0def List(): print List #present the Listdef representInt(s): #check if value is integer try: int(s) return True except ValueError: return Falsedef 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." % ydef 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时,它会尝试检查项目是否在函数中.你不能在函数上使用,因此错误.解决方案很简单:使用更清晰的名称! 总结 以上是内存溢出为你收集整理的python – :TypeError:’function’类型的参数不可迭代“,但在单独测试时有效全部内容,希望文章能够帮你解决python – :TypeError:’function’类型的参数不可迭代“,但在单独测试时有效所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)