on python 3
Hi, I have a stock of products (as a dictionary. where each key is the identification number and each value is a dictionary of information). The program takes in a identification number (product wanted) and the desired amount. Then the code is supposed to add the product info into a empty basket, edit the number in stock to the original minus the inputted amount. and edit the amount in the basket to the amount desired.
however the code keeps making the amount of the product in the stock output zero. And the amount of the product in the basket to output zero too.
via the print command I think I have located the problem. somewhere in the if statement below.
but it looks so simple I don't know where I'm going wrong !
Thanks very much in advance. the stock and a 'pretty' print function are at the top of the code, to help you visualise the situation
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]
That's your problem, there are mutable types, so this don't make two identical things but one thing with two different names.
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
To do what you want you have to do say explicitly that you want a copy :
basket[ident] = stock[ident].copy()