我是Python 2的新手。我正在尝试构建一个“购物车”Python程序,但坚持“在购买之前检查库存”步骤。
首先,我在这里阅读了许多相同问题的主题,但它们似乎与我的原因不同。
其次,我已经将每个函数分开并在另一个文件中进行测试。他们个人工作得很好。但是当加入 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)
函数时,它会给出错误。
我认为问题来自于“in”命令。
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()
以下是代码的其余部分,如果有帮助的话:
Email[i]+=i;
答案 0 :(得分:5)
您创建了一个名为list
的列表(您不应该这样做,因为它已经是内置名称),但是您还创建了一个名为list
的函数(同样,不要这样做)这个)。 list
现在指的是功能,而不是您的列表。
因此,当您检查y in list
时,它会尝试检查项目是否在函数中。您不能在函数上使用in
,因此会出错。解决方案很简单:为事物使用更清晰的名称!!
答案 1 :(得分:1)
lst = ['apple', 'banana', 'cat', 'dog', 'elephant', 'flamingo', 'goofy', 'ham']
a = 0
list
是python中的预定义函数,因此请使用其他名称。
答案 2 :(得分:1)
首先,不要为列表list
命名!其次,功能名称越具描述性越好。所以这里有一些编辑了名称的代码:
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()
第二件事,与您的问题无关:当您要求购买的东西时,它会打印清单......并结束程序。我建议做一些事情来阻止程序在打印可用内容列表后立即结束。也许甚至可以在最开始打印该列表。
希望这有帮助!