添加到购物篮,并从库存中取出等价物
Adding to a shopping basket, and taking the equivalent from the stock
在 python 3
你好,我有一个产品库存(作为一个字典。其中每个键是标识号,每个值是一个信息字典)。该程序接受一个标识号(需要的产品)和所需的数量。
然后代码应该将产品信息添加到一个空篮子中,将库存数量编辑为原始数量减去输入的数量。并将篮子中的数量编辑为所需的数量。
但是代码一直使库存输出中的产品数量为零。并且篮子里的产品数量也输出为零。
通过打印命令我想我找到了问题所在。在下面的 if 语句中的某处。
但是它看起来很简单我不知道我哪里错了!
非常感谢。股票和 'pretty' 打印功能位于代码顶部,以帮助您直观地了解情况
stock = {
'10005' : {
'name' : 'Conference Pears Loose',
'price' : 2.00,
'unit' : 'kg',
'promotion' : None,
'group' : None,
'amount' : 1.550
},
'10013' : {
'name' : 'Emmental Slices 250G',
'price' : 1.75,
'unit' : 'pieces',
'promotion' : 'get2pay1',
'group' : None,
'amount' : 9
},
'10015' : {
'name' : 'Diced Beef 400G',
'price' : 4.50,
'unit' : 'pieces',
'promotion': 'get4pay3',
'group' : 4,
'amount' : 14
}}
def listitems(dct):
"""
inputs dictionary of stock and prints a lovely table showing all the items with info
"""
print("\n")
print(" {0:^5} | {1:<38} | {2:^7} | {3:^11} ".format("Ident", "Product", "Price", "Amount"))
print("-" *7 + "+" + "-" * 40 + "+" + "-" * 9 + "+" + "-"*12 + "+")
for key in sorted(dct):
print(" {:^5} | {name:<38} | {price:>5.2f} £ | {amount:>} {unit:<14}".format(key, **dct[key]))
return
#main code of function
basket = dict()
quantity = input("Number of items? ")
#amount = 6
ident = input("Indentification number? ")
#ident = "10011"
listitems(stock)
try:
quantity = int(quantity)
except ValueError:
try:
quantity = float(quantity)
except ValueError:
print("You have entered a invalid amount")
try:
ident = str(ident)
except ValueError:
print("you have entered a invalid indent.")
#print("amount is ", quantity)
#print("amount in stock ", stock[ident]["amount"])
if quantity > 0:
if quantity < stock[ident]["amount"]:
basket[ident] = stock[ident] #adding the product tp the basket.
basket[ident]["amount"] = quantity
stock[ident]["amount"] = stock[ident]["amount"] - quantity
listitems(stock)
listitems(basket)
print("amount is ", quantity)
print("amount in stock ", stock[ident]["amount"])
print("amount in basket", basket[ident]["amount"])
basket[ident] = stock[ident]
那是你的问题,有可变类型,所以这不是两个相同的东西,而是一个有两个不同名称的东西。
basket[ident]["amount"] = quantity # ok with that
# since basket[ident] and stock[ident] are now the same you just subtract the amount you just set to itself
stock[ident]["amount"] = stock[ident]["amount"] - quantity
要想做你想做的事,你必须明确地说你想要一份副本:
basket[ident] = stock[ident].copy()
在 python 3
你好,我有一个产品库存(作为一个字典。其中每个键是标识号,每个值是一个信息字典)。该程序接受一个标识号(需要的产品)和所需的数量。 然后代码应该将产品信息添加到一个空篮子中,将库存数量编辑为原始数量减去输入的数量。并将篮子中的数量编辑为所需的数量。
但是代码一直使库存输出中的产品数量为零。并且篮子里的产品数量也输出为零。
通过打印命令我想我找到了问题所在。在下面的 if 语句中的某处。
但是它看起来很简单我不知道我哪里错了!
非常感谢。股票和 'pretty' 打印功能位于代码顶部,以帮助您直观地了解情况
stock = {
'10005' : {
'name' : 'Conference Pears Loose',
'price' : 2.00,
'unit' : 'kg',
'promotion' : None,
'group' : None,
'amount' : 1.550
},
'10013' : {
'name' : 'Emmental Slices 250G',
'price' : 1.75,
'unit' : 'pieces',
'promotion' : 'get2pay1',
'group' : None,
'amount' : 9
},
'10015' : {
'name' : 'Diced Beef 400G',
'price' : 4.50,
'unit' : 'pieces',
'promotion': 'get4pay3',
'group' : 4,
'amount' : 14
}}
def listitems(dct):
"""
inputs dictionary of stock and prints a lovely table showing all the items with info
"""
print("\n")
print(" {0:^5} | {1:<38} | {2:^7} | {3:^11} ".format("Ident", "Product", "Price", "Amount"))
print("-" *7 + "+" + "-" * 40 + "+" + "-" * 9 + "+" + "-"*12 + "+")
for key in sorted(dct):
print(" {:^5} | {name:<38} | {price:>5.2f} £ | {amount:>} {unit:<14}".format(key, **dct[key]))
return
#main code of function
basket = dict()
quantity = input("Number of items? ")
#amount = 6
ident = input("Indentification number? ")
#ident = "10011"
listitems(stock)
try:
quantity = int(quantity)
except ValueError:
try:
quantity = float(quantity)
except ValueError:
print("You have entered a invalid amount")
try:
ident = str(ident)
except ValueError:
print("you have entered a invalid indent.")
#print("amount is ", quantity)
#print("amount in stock ", stock[ident]["amount"])
if quantity > 0:
if quantity < stock[ident]["amount"]:
basket[ident] = stock[ident] #adding the product tp the basket.
basket[ident]["amount"] = quantity
stock[ident]["amount"] = stock[ident]["amount"] - quantity
listitems(stock)
listitems(basket)
print("amount is ", quantity)
print("amount in stock ", stock[ident]["amount"])
print("amount in basket", basket[ident]["amount"])
basket[ident] = stock[ident]
那是你的问题,有可变类型,所以这不是两个相同的东西,而是一个有两个不同名称的东西。
basket[ident]["amount"] = quantity # ok with that
# since basket[ident] and stock[ident] are now the same you just subtract the amount you just set to itself
stock[ident]["amount"] = stock[ident]["amount"] - quantity
要想做你想做的事,你必须明确地说你想要一份副本:
basket[ident] = stock[ident].copy()