购物车优化

  终于,终于,在自己这几天无数次跳坑之后,终于爬出来了。把里面所有的问题都修正了。

  程序显的有些繁琐,但是对于我这个水平来说,已经是很高兴了,毕竟学了并不是很久,供新手学习还是有些帮助的。自己肯定也会越来越努力,好好学习,加油!

  1 __Author__ = "Zhang Peng"
  2 product_list = [
  3     ('Iphone', 5888),
  4     ('笔记本', 12888),
  5     ('手表', 10600),
  6     ('电视', 3888),
  7     ('', 58),
  8 ]
  9 
 10 id_list = ["商家", "用户", "shangjia", "yonghu"]
 11 id_shopping = input("Please input your ID:\n")
 12 
 13 exit_flag = False
 14 
 15 if id_shopping == "shangjia":
 16     choice = input("你是否想添加\修改商品?添加商品输入'1',修改商品输入'2',退出输入'q':\n")
 17     if choice == "1":
 18         while not exit_flag:
 19             name = input("请输入您想添加的商品名称:\n")
 20             name2 = input("请输入您添加商品的价格:\n")
 21             if name == "q" or name2 == "q":
 22                 exit_flag = True
 23             else:
 24                 product_list.append((name, name2), )
 25 
 26     elif choice == "2":
 27         while not exit_flag:
 28             name3 = input("请输入您想修改价格的商品名称:\n")
 29             name4 = input("请输入您想要修改的价格:\n")
 30 
 31             if name3 == "q" or name4 == "q":
 32                 exit_flag = True
 33             else:
 34                 a = product_list.index(name3)
 35                 product_list.remove(name3)
 36                 product_list.index((name3, name4))
 37 
 38     else:
 39         if choice == "q":
 40             exit_flag = True
 41     print(product_list)
 42 
 43 
 44 elif id_shopping == "yonghu":
 45     shopping_list=[]
 46     with open("shopping_list.txt","r",encoding="utf8") as f_shop1:
 47         print("你的购物车里已经有:")
 48         print(f_shop1.read())
 49     with open("salary.txt","r",encoding="utf8") as f_salary1:
 50         salary = f_salary1.read()
 51         print(salary)
 52     if salary.isdigit():  # isdigit 是判断输入的是不是一个数字
 53         salary = int(salary)
 54         while True:
 55             print("我们有一下商品:\n")
 56             for index, item in enumerate(product_list):
 57                 print(index, item)
 58             user_choice = input("请问你还想买什么?:\n")
 59             if user_choice.isdigit():
 60                 user_choice = int(user_choice)
 61                 if user_choice < len(product_list) and user_choice > 0:
 62                     p_item = product_list[user_choice]
 63                     if p_item[1] < salary:  # 买不起
 64                         shopping_list.append(p_item)
 65                         salary -= p_item[1]
 66                         print("您买的商品已经加入购物车")
 67                     else:
 68                         print("没钱别嘚瑟了")
 69                 else:
 70                     print("没有您要的商品")
 71             elif user_choice == 'q':
 72                 print("----------shopping list----------")
 73                 for p in shopping_list:
 74                     print(p)
 75                     with open("shopping_list.txt", "a", encoding="utf8") as f_shop2:
 76                         f_shop2.write(str(p))
 77                 print("你还剩余", salary)
 78                 with open("salary.txt", "w", encoding="utf8") as f_salary2:
 79                     f_salary2.write(str(salary))
 80                 exit()
 81 
 82             else:
 83                 print("输入错误")
 84 
 85 else:
 86     shopping_list = []
 87     salary = input("请输入你的月薪:\n")
 88     if salary.isdigit():  # isdigit 是判断输入的是不是一个数字
 89         salary = int(salary)
 90         while True:
 91             for index, item in enumerate(product_list):
 92                 print(index, item)
 93             user_choice = input("请问你想买什么?:\n")
 94             if user_choice.isdigit():
 95                 user_choice = int(user_choice)
 96                 if user_choice < len(product_list) and user_choice > 0:
 97                     p_item = product_list[user_choice]
 98                     if p_item[1] < salary:  # 买不起
 99                         shopping_list.append(p_item)
100                         salary -= p_item[1]
101                         print("您买的商品已经加入购物车")
102                     else:
103                         print("没钱别嘚瑟了")
104                 else:
105                     print("没有您要的商品")
106             elif user_choice == 'q':
107                 print("----------shopping list----------")
108                 for p in shopping_list:
109                     print(p)
110                     with open("shopping_list.txt", "a",encoding="utf8") as f_shop3:
111                         f_shop3.write(str(p))
112                 print("你还剩余", salary)
113                 with open("salary.txt", "w",encoding="utf8") as f_salary3:
114                     f_salary3.write(str(salary))
115                 exit()
116 
117             else:
118                 print("输入错误")

   希望能得到打什么的评论,这才是我最大的学习动力!谢谢!

posted @ 2017-10-20 13:11  黄舒砚  阅读(379)  评论(0编辑  收藏  举报