- 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
-