知识点: 文件读,写操作,if 判断, for 循环

salary = input("输入你的工资:")
bought_list = []
product_list = {}

with open("product_list","r",encoding="utf-8") as f1:
    for item in f1:
        p_name,p_price = item.strip().split(':')
        product_list[p_name]=int(p_price)
print(product_list)

if salary.isdigit():
    salary = int(salary)
    while True:
        user_choice = input("please input product which you want:")
        if user_choice in product_list.keys():
            if product_list[user_choice] <= salary:
                bought_list.append(user_choice) #增加列表的元素
                salary = salary - product_list[user_choice]
                print("Had bought  [\033[32;1m%s\033[0m], and your balance is \033[31;1m%s\033[0m"%(user_choice,salary))
                print(bought_list)
            else:
                print("Your balance is less than product's price")
                continue
        elif user_choice is 'q':
            with open('bought.txt','w+',encoding='utf-8') as f2:
                for goods in bought_list:
                    print(goods,file=f2)
            exit(print("You had bought  %s goods, and your balance is %s"%(bought_list, salary)))

        else:
            print("The good had been sold out")
View Code

相关文章:

  • 2021-12-13
  • 2022-03-10
  • 2021-09-02
  • 2022-12-23
  • 2021-10-29
  • 2021-09-21
  • 2022-01-13
  • 2021-09-03
猜你喜欢
  • 2022-01-15
  • 2021-08-26
  • 2021-11-27
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2022-01-15
相关资源
相似解决方案