从python中的字典中提取元组值

是M

我正在做一个餐厅项目,我需要一只手 :) 所以程序将显示主菜,用户必须输入他/她想要的菜......然后选择然后添加到账单中(菜名,价格,商品数量)...到目前为止,我选择了一个字典,因此当用户输入 1(键)时,程序将显示 Mashroum Risto...

这创造了什么:

dishes = {1 : ('Mashroum Risito', 3.950), 2 : ['Tomato Pasta', 2.250],3:['Spagehtie',4.850]}

现在我的问题是如何在没有价格的情况下获取菜名(价格为3.950)并提取它!以及如何在没有名称的情况下获取价格,然后我可以将其发送到 bill 函数中进行计算?如果您有任何建议,请继续,因为我不知道使用字典是否是正确的选择

def MainDish():

dishes = {1 : ('Mashroum Risito', 3.950), 2 : ['Tomato Pasta', 2.250],3: 
['Spagehtie',4.850]}

dishes.values
print("1. Mashroum Risito       3.950KD")
print("2. Tomato Pasta          2.250KD")
print("3. Spagehtie             4.850KD")
choice = eval(input('Enter your choice: '))
NumOfItem = eval(input('How many dish(es): '))

while(choice != 0):
    print(dishes.get(choice)) #to display the item only without the 
    price
    a = dishes.values()
    recipient(a)
    break
唐·阿贝拉特

你实现它的方式:

print (dishes[1][0]) #will give you name
print (dishes[1][1]) #will give you price

在哪里 [x][y]

x = 字典中的键(在您的情况下输入)

y = dict 中值的元素(0 = 名称,1 = 价格在您的情况下)

您可能应该更好地创建字典,如下所示:

后续问题不太清楚,但我认为这就是您大致追求的。您需要调整使用的回报:

def MainDish():

    NumOfItem = float(input('How many dish(es): '))
    dish = list(dishes)[choice-1]
    cost_of_dish = dishes[dish]
    totalcost = cost_of_dish * NumOfItem
    print (f"\n\tOrdered {NumOfItem}x {dish} at {cost_of_dish}KD each. Total = {totalcost}KD\n")

    return dish, cost_of_dish, totalcost

dishes = {'Mashroum Risito': 3.950, 'Tomato Pasta': 2.250}
for key,val in dishes.items():
    print (f"{list(dishes).index(key)+1}. {key}: {val}KD")


keepgoing = True
while keepgoing:
    choice = int(input('Enter your choice: '))
    if choice == 0:
        keepgoing = False
        break
    else:
        dish, cost_of_dish, totalcost = MainDish()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章