[英]How to update user input result in while loop?
我正在嘗試向用戶提供有關該產品的信息,但是如果他們向訂單中添加了其他產品,我該如何提供給他們?
import datetime
db = [
{
'product': 'cola',
'price': {
'USD': '2',
'GEL': '6'
},
'amount': 20,
'validity': '17/12/2019'
},
{
'product': 'cake',
'price': {
'USD': '3',
'GEL': '9'
},
'amount': 15,
'validity': '17/12/2019'
},
{
'product': 'tea',
'price': {
'USD': '1',
'GEL': '3'
},
'amount': 14,
'validity': '17/12/2019'
},
]
amount_of_product = {}
validity_of_product = {}
prices_of_product = {}
for i in db:
amount_of_product.update({i["product"]: i["amount"]})
validity_of_product.update({i["product"]: i["validity"]})
prices_of_product.update({i["product"]: i["price"]})
adLoop = True
final_price = []
while adLoop:
user_input = input("Please enter a product name: ")
if user_input in amount_of_product.keys() and validity_of_product.keys():
print(f"Currently, we have {amount_of_product[user_input]} amount of {user_input} left, "
f"which are valid through {validity_of_product[user_input]}")
user_input_two = int(input("Please enter the amount: "))
user_input_three = input("In which currency would you like to pay in?(GEL or USD: ").upper()
price = prices_of_product[user_input][user_input_three]
total = user_input_two * int(price)
if user_input_three == "GEL":
final_price.append(total)
print(f"Your order is: {user_input_two} {user_input} and total price for it is: {total}₾")
elif user_input_three == "USD":
final_price.append(total * 3)
print(f"Your order is: {user_input_two} {user_input} and total price for it is: {total}$")
stop_or_order = input("Would you like to add anything else?: ")
if stop_or_order == "yes":
adLoop = True
elif stop_or_order == "no":
adLoop = False
因此,如果用戶訂購可樂和蛋糕,我希望輸出如下所示:您的訂單是 1 個可樂和 1 個蛋糕,總價為: sum(final_price)
但是每次我執行代碼時,舊的輸入都會被刪除,結果我得到了新的輸入。 我想將它保存在某個地方並向用戶展示他/她訂購的所有內容。
您在循環內定義user_input ,因此每次迭代都會被覆蓋。 你可以定義一個字典
user_shopping_cart = {
'cola':0,
'cake':0,
'tea':0
}
在 while 循環之前,並在用戶將商品放入購物車時更新購物車,然后使用購物車中的數據生成輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.