写代码:购物车程序

以下购物车程序v1版本,是先基本功能要求:


  1 # enconding: utf-8
  2 # 数据结构:
  3 # goods = [
  4 # {"name": "电脑", "price": 1999},
  5 # {"name": "鼠标", "price": 10},
  6 # {"name": "游艇", "price": 20},
  7 # {"name": "美女", "price": 998},
  8 # ......
  9 # ]
 10 #
 11 # 功能要求:
 12 # 基础要求:
 13 #
 14 # 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
 15 #
 16 # 2、允许用户根据商品编号购买商品
 17 #
 18 # 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
 19 #
 20 # 4、可随时退出,退出时,打印已购买商品和余额
 21 #
 22 # 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
 23 #
 24 #
 25 # 扩展需求:
 26 #
 27 # 1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
 28 #
 29 # 2、允许查询之前的消费记录
 30 
 31 goods = [
 32     {"name": "电脑", "price": 1999},
 33     {"name": "鼠标", "price": 10},
 34     {"name": "游艇", "price": 20},
 35     {"name": "美女", "price": 998},
 36 ]
 37 
 38 account = {"alex": "123", "emily": "123", "yanfu": "123"}
 39 
 40 username = input("请输入用户名: ").strip()
 41 password = input("请输入密码: ").strip()
 42 list_all = []  # 存放所有商品
 43 shopping_list = []  # 空列表,存放购买的商品
 44 
 45 if username not in account or password != account[username]:
 46     print("用户名密码有错误,请检查后再试!")
 47     exit()
 48 
 49 salary = input("请输入你的工资: ")
 50 if salary.isdigit() is True:
 51     salary = int(salary)
 52 else:
 53     print("工资非数字,请从新输入: ")
 54 
 55 while True:
 56     for info in goods:
 57         list_all.append([info['name'], info['price']])  # 把商品信息添加到列表
 58 
 59     while True:
 60         print("可选商品如下:")
 61         for index, i in enumerate(list_all):  # index作为下标索引
 62             print(index, i)
 63         user_choice = input("请输入你要购买的商品:")
 64         if user_choice == "n":
 65             if shopping_list is None:
 66                 print("购物车为空")
 67             else:
 68                 print("---------购物车-----------")
 69                 for s_index, s in enumerate(shopping_list):
 70                     print(s_index, s)
 71                 print("-------------------------")
 72         elif user_choice.isdigit():
 73             user_choice = int(user_choice)
 74             if user_choice < len(list_all) and user_choice >= 0:
 75                 product_choice = list_all[user_choice]
 76                 if product_choice[1] < salary:
 77                     shopping_list.append(product_choice)  # 买得起,就放入购物车
 78                     salary -= product_choice[1]
 79                     print(
 80                         "添加\033[33;1m{}\033[0m到您的购物车,您剩余工资为 \033[33;1m{}\033[0m".format(
 81                             product_choice, salary))
 82                 else:
 83                     print(
 84                         "\033[41;1m你的余额只剩{}\033[0m, 商品不能添加购物车".format(salary))
 85                     print("---------购物车-----------")
 86                     for s_index, s in enumerate(shopping_list):
 87                         print(s_index, s)
 88                     print("你的余额为:\033[31;1m{}\033[0m".format(salary))
 89                     print("-------------------------")
 90             else:
 91                 print("没有这个商品,请检查从新输入。")
 92         elif user_choice == "q":
 93             print("---------购物车-----------")
 94             for s_index, s in enumerate(shopping_list):
 95                 print(s_index, s)
 96             print("\033[31;1m你的余额为{}\033[0m".format(salary))
 97             exit()
 98 
 99         else:
100             print("输入错误")

以下是购物车程序v2版本,实现扩展需求,但还有很多方面需要完善。

  1 # enconding: utf-8
  2 import os
  3 
  4 # 数据结构:
  5 # goods = [
  6 # {"name": "电脑", "price": 1999},
  7 # {"name": "鼠标", "price": 10},
  8 # {"name": "游艇", "price": 20},
  9 # {"name": "美女", "price": 998},
 10 # ......
 11 # ]
 12 #
 13 # 功能要求:
 14 # 基础要求:
 15 #
 16 # 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
 17 #
 18 # 2、允许用户根据商品编号购买商品
 19 #
 20 # 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
 21 #
 22 # 4、可随时退出,退出时,打印已购买商品和余额
 23 #
 24 # 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
 25 #
 26 #
 27 # 扩展需求:
 28 #
 29 # 1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
 30 #
 31 # 2、允许查询之前的消费记录
 32 
 33 goods = [
 34     {"name": "电脑", "price": 1999},
 35     {"name": "鼠标", "price": 10},
 36     {"name": "游艇", "price": 20},
 37     {"name": "美女", "price": 998},
 38 ]
 39 
 40 account = {"alex": "123", "emily": "123", "yanfu": "123"}
 41 
 42 username = input("请输入用户名: ").strip()
 43 password = input("请输入密码: ").strip()
 44 list_all = []  # 存放所有商品
 45 shopping_list = []  # 空列表,存放购买的商品
 46 
 47 if username in account and password == account[username]:
 48     if os.path.getsize('{}_price.txt'.format(username)) == 0:
 49         salary = input("请输入你的工资: ")
 50         if salary.isdigit() is True:
 51             salary = int(salary)
 52         else:
 53             print("工资非数字,请从新输入: ")
 54     else:
 55         with open('{}.txt'.format(username), 'r+', encoding='utf-8') as f, \
 56                 open('{}_price.txt'.format(username), 'r', encoding='utf-8') as p:
 57             f = f.read().strip()
 58             k = eval(f)
 59             shopping_list.append(k)
 60             salary = p.read().strip()
 61             salary = int(salary)
 62             print('#################购物清单###############')
 63             for i in shopping_list:
 64                 print(i)
 65                 print("\n")
 66             print('剩余金额:{}'.format(salary))
 67 else:
 68     print("用户名密码有错误,请检查后再试!")
 69     exit()
 70 
 71 while True:
 72 
 73     for info in goods:
 74         list_all.append([info['name'], info['price']])  # 把商品信息添加到列表
 75 
 76     while True:
 77         print("可选商品如下:")
 78         for index, i in enumerate(list_all):  # index作为下标索引
 79             print(index, i)
 80         user_choice = input("请输入你要购买的商品:")
 81         if user_choice == "n":
 82             if shopping_list is None:
 83                 print("购物车为空")
 84             else:
 85                 print("---------购物车-----------")
 86                 for s in enumerate(shopping_list):
 87                     print(s)
 88                 print("-------------------------")
 89         elif user_choice.isdigit():
 90             user_choice = int(user_choice)
 91             if user_choice < len(list_all) and user_choice >= 0:
 92                 product_choice = list_all[user_choice]
 93                 if product_choice[1] < salary:
 94                     shopping_list.append(product_choice)  # 买得起,就放入购物车
 95                     salary -= product_choice[1]
 96                     print(
 97                         "添加\033[33;1m{}\033[0m到您的购物车,您剩余工资为 \033[33;1m{}\033[0m".format(
 98                             product_choice, salary))
 99                 else:
100                     print("\033[41;1m你的余额只剩{}\033[0m, 商品不能添加购物车".format(salary))
101                     print("---------购物车-----------")
102                     for s in enumerate(shopping_list):
103                         print(s)
104                     print("你的余额为:\033[31;1m{}\033[0m".format(salary))
105                     print("-------------------------")
106             else:
107                 print("没有这个商品,请检查从新输入。")
108         elif user_choice == "q":
109             print("---------购物车-----------")
110             for s in enumerate(shopping_list):
111                 print(s)
112                 with open("{}.txt".format(username), 'w', encoding='utf-8') as f:
113                     f.write(str(shopping_list[:]) + "\n")
114 
115                 with open('{}_price.txt'.format(username), 'w', encoding='utf-8') as f:
116                     f.write(str(salary) + "\n")
117             print("你的余额为:\033[31;1m{}\033[0m".format(salary))
118             print("-------------欢迎再次光临-------------")
119             exit()
120 
121         else:
122             print("输入错误")

 

 
posted @ 2018-11-29 11:06  demilyc  阅读(532)  评论(0编辑  收藏  举报