简体   繁体   English

如何将值添加到python字典中的现有值?

[英]How to add on value to an existing value that is in a dictionary in python?

I am once again seeking for your help in python where I am trying to add a value to an existing dictionary.我再次在 python 中寻求您的帮助,我正在尝试向现有字典添加值。 Below I have my dictionary of items and a blank dictionary.下面我有我的项目字典和一个空白字典。

Dairy_goods = {1:{'item':'Milk','p':2.47,'g':0.16,'offer':'Yes','total':0},
        2:{'item':'Butter','p':4.50,'g':0.32,'offer':'No','total':0},
        3:{'item':'Egg','p':3.40,'g':0.24,'offer':'No','total':0}}

shopping_basket={}

I then set a if condition where if 'Milk' is selected it will be added into the shopping_basket{} dictionary然后我设置了一个 if 条件,如果选择了“Milk”,它将被添加到shopping_basket{}字典中

choose=int(input('1.Item= Milk, Price= $2.47, GST= $0.16, Offer=Yes\n'
                 '2.item= Butter, Price= $4.50, GST= $0.32, Offer=No\n'
                 '3.Item= Egg, Price= $4.50, GST= $0.32, Ofer=No\n'
                 'Enter your option: '))
qnty=int(input('How many do you want?: '))
price=(Dairy_goods[choose]['p'])
y = ((Dairy_goods[choose]['p']) * qnty)
gst = (Dairy_goods[choose]['g'])
offer = (Dairy_goods[choose]['offer'])

if choose==1:
    shopping_basket['Milk'] ={'Quantity': 0 + qnty, 'Individual price': price, 'total': y, 'GST': gst, 'offer': offer}
    # cart[Menu[choose]['item']] = cart.get(Menu[choose]['item'], 0) + qnty

But lets say I have already selected Milk once with a Quantity of 2, and if I would want to select it again to add on to the existing Quantity, it will not add on and would simply reset and give me the Value of the new keyed in quantity.但是假设我已经选择了一次牛奶,数量为 2,如果我想再次选择它以添加到现有数量,它不会添加并且只会重置并为我提供新键控的值在数量上。

#example of output
How many Milk do you want?: 2
{'Milk': {'Quantity': 2, 'Individual price': 2.47, 'total': 2.47, 'GST': 0.16, 'offer': 'Yes'}}

#selecting Milk again

How many Milk do you want? : 3 
{'Milk': {'Quantity': 3, 'Individual price': 2.47, 'total': 2.47, 'GST': 0.16, 'offer': 'Yes'}} 
# at this point I should have a quantity for 5 but it does not add on to the existing value.

anyway you could do something like this using loops无论如何你可以使用循环来做这样的事情

Dairy_goods = {
    1: {"item": "Milk", "p": 2.47, "g": 0.16, "offer": "Yes", "total": 0},
    2: {"item": "Butter", "p": 4.50, "g": 0.32, "offer": "No", "total": 0},
    3: {"item": "Egg", "p": 3.40, "g": 0.24, "offer": "No", "total": 0},
}
shopping_basket = {}

while True:
    choose = int(
        input(
            "\n\n1.Item= Milk, Price= $2.47, GST= $0.16, Offer=Yes\n"
            "2.item= Butter, Price= $4.50, GST= $0.32, Offer=No\n"
            "3.Item= Egg, Price= $4.50, GST= $0.32, Ofer=No\n"
            "0.Exitthe program \n"
            "Enter your option: "
        )
    )

    if choose == 0:
        print("thanks!")
        break

    qnty = int(input("How many do you want?: "))
    price = Dairy_goods[choose]["p"]
    y = Dairy_goods[choose]["p"] * qnty
    gst = Dairy_goods[choose]["g"]
    offer = Dairy_goods[choose]["offer"]

    if choose == 1:
        if shopping_basket.get("Milk"):
            old_quan = shopping_basket["Milk"]["Quantity"]
        else:
            old_quan = 0
        shopping_basket["Milk"] = {
            "Quantity": old_quan + qnty,
            "Individual price": price,
            "total": y,
            "GST": gst,
            "offer": offer,
        }

    print(shopping_basket)

values will be reset on rerunning the program , to avoid this store it on file for that have look at this it may helps you more.值将在重新运行程序复位,避免对文件,这个店就已经看清楚了这个它可能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM