import os

dict01 = {
'iphone' : {
'5999' : {
'總部位於美國' : '價格相對較貴',
},
},
'wahaha' : {
'15' : {
'總部位於中國' : '經濟實惠,大眾喜愛',
},
},
'MacBook Air' : {
'15000' : {
'蘋果公司筆記本' : '效能優越,續航驚人',
},
},
'礦泉水' : {
'2' : {
'產自山區,富含礦物質' : '解渴,人人需要',
},
}, } if os.path.exists('money.txt') :
with open('money.txt', "r", encoding='utf-8-sig') as m01:
budget = m01.read()
budget = budget.strip()
answer = input("當前預算為{0},是否增加預算?(y/n)".format(budget))
if answer == 'y' :
budgetNew = input("你的預算是多少?")
budget = int(budgetNew) + int(budget)
print("目前預算為{0}".format(budget))
else :
budget = input("你的預算是多少?") Continue = False
shoppingCart = []
budget = int(budget) while not Continue :
# budget 購買物資預算
print("商品清單如下:")
# 將商品名稱取出,儲存到列表中
product = list(dict01.keys())
# 遍歷輸出所有的商品
for index,list01 in enumerate(product):
print(index,list01,list(dict01[list01].keys())[0] + "元")
choice = input("請輸入你的選擇是:")
# 判斷輸出值是否為數字
if choice.isdigit():
# 字串轉換為數字型別
choice = int(choice)
if choice < len(product) and choice >= 0:
# 獲取商品價格
shoppingName = product[choice]
# print(shoppingName)
choicePrice = int(list(dict01[shoppingName].keys())[0])
if budget >= choicePrice :
# 選購商品儲存在列表中
shoppingCart.append(product[choice])
budget -= choicePrice
print("你的餘額還有" + str(budget) + "元。")
else :
print("你買不起這個商品!")
else :
print("沒有這個商品,請重新選擇。")
break
elif choice == 'q':
Continue = True
print("\n你購買的清單如下:")
print(shoppingCart)
print("你的餘額還有" + str(budget) + "元。\n")
with open('money.txt', "w", encoding='utf-8-sig') as m :
m.write(str(budget))
break
else :
print("請重新輸入,按q退出!")
break