首页 > 解决方案 > 将字典中的单个值添加到空字典中

问题描述

Products = {
    "BUTTER": (4.50,False),
    "BREAD": (2.70,False),
    "BUTTON MUSHROOMS": (1.15,False),
    "BAKING BEAN": (1.35,True),
    "CEREAL": (7.00,True),
    "CRACKERS": (3.10,False),
    "EGGS": (3.40,False),
    "MILK": (2.30,True),
    "TOMATO": (1.45,True),
}
    print("Name".ljust(padding), f'{"Price":>12}', f'{"Membership Applicable":>25}')

    for name, (price, val) in Products.items():

        print(name.ljust(padding), f"{price:>10}",f"{str(val):>18}")

def addItem():
    itemlist()
    getselection = input(
        "Enter an item from the list above that u would like to add to cart :"
    )
    quantity = int(input("Enter how many of this item u would like to add to cart: "))
    if quantity < 0:
        print("Invalid quantity input")

    elif getselection.upper() in Products:
        shoppingCart[getselection.upper()] = quantity
        print("\n", quantity, "of", getselection, "has been added to cart !\n",)

    else:
        print("Item not found")

addItem()

你好 !我正在尝试将数量名称成员资格(真/假)和价格添加到我的空字典中,到目前为止我可以添加数量和名称,因为它们以输入的形式但是我知道如何从产品字典中获取固定值

标签: pythonpython-2.7dictionaryinputboolean

解决方案


推荐阅读