簡體   English   中英

Python:練習雜貨清單 - 有關的問題

[英]Python: Practicing A Grocery List - Question regarding

我希望讓用戶從項目列表中選擇。 我想我可以問用戶指定數量的問題(“輸入您的項目:”)並將所有問題加起來(如 q1、q2、q3 等)。

但我寧願讓用戶選擇無限數量的項目,直到他們按下 Enter 並打破循環。 然后添加他們條目中的所有價格,只要它與fmPrices dict 匹配。

import math


dollar = "$"

items = ["Eggs","Milk","Chips","Butter","Grapes","Salsa","Beer"]
items.sort() 

choice = ("Choose from the following items:")
print (choice)
print (items)

fmPrices = {
    "Eggs" : 2.99,
    "Milk": 3.99,
    "Chips": 4.29,
    "Butter": 3.29,
    "Grapes": 3.49,
    "Salsa": 40.99,
    "Beer": 1.99

}

while True:
    q1 = input("Enter your item: ")

    if q1 == '':
        break
    else: 
        input("Enter your item: ")


print ("Your estimated cost is: ")
total = round(fmPrices[q1],2)

print ("{}""{}" .format(dollar, total))
mylist = []
total = 0
while True:
    q1 = input("Enter your item: ")
    if q1 == '':
        break
    if q1 not in fmPrices:
        print("Item is not in the list")
    else:
        mylist.append(q1)
        total += fmPrices[q1]

print ("Your estimated cost is: ")
print( "$", round(total,2) )

例如

cost = 0

while True:
    q1 = input("Enter your item: ")
    if q1 == '':
        break
    price = fmPrices.get(q1)
    if price == None:
        print("Item not available")
    else:
        cost += price 

total = round(cost,2)
print(f"Your estimated cost is: {dollar}{total}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM