购物车

goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
......
]
功能要求:
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示

扩展需求:
1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
2、允许查询之前的消费记录

3、用户可以添加商品

  1 #!/usr/bin/env python3
  2 #-*- coding:utf-8 -*-
  3 '''
  4 Time:2018/5/4 18:11
  5 File:购物车优化
  6 '''
  7 
  8 
  9 exit_flag = False
 10 
 11 goods = [
 12 {"name": "电脑", "price": 1999},
 13 {"name": "鼠标", "price": 10},
 14 {"name": "游艇", "price": 20},
 15 {"name": "美女", "price": 998},
 16 ]
 17 
 18 shopping = []
 19 name = input('请输入您的账号:').strip()
 20 f = open('black_file.txt','r',encoding='utf-8')
 21 lock_file = f.read()# 读取黑名单文件
 22 f.close()
 23 if name in  lock_file:# 如果账户在黑名单文件里,账户将被锁定
 24     print('账户被锁定,请联系管理员!')
 25     exit()
 26 
 27 cont = 0
 28 with open('b.txt', 'r', encoding='utf-8') as f:
 29         yi = eval(f.read())
 30 
 31 while cont < 3:
 32     if name not in yi['name']:
 33         print('账号不存在,请注册下你的账号!')
 34         name1 = input('请输您的账号:').strip()
 35         passwd1 = input('请输入您的密码:').strip()
 36         while True:
 37             salary = input('请输入您的工资:').strip()
 38             if salary.isdigit():
 39                 salary = int(salary)
 40                 break
 41             else:
 42                 print('输入错误')
 43                 continue
 44         lis = {'name': name1, 'passwd': passwd1, 'salary': salary}
 45         with open('b.txt', 'w', encoding='utf8') as f:
 46                 f.write(str(lis))
 47                 break
 48     else:
 49         passwd = input('请输入您的密码:').strip()
 50         if name == yi['name'] and passwd == yi['passwd']:
 51             print('欢迎回来\033[31;1m%s\033[0m,您的余额还有\033[31;1m%s\033[0m'%(name,yi['salary']))
 52             shopping_list = input("是否要读取上一次购物历史|y|n|:").strip()
 53             if shopping_list == 'y':
 54                 with open('a.txt','r',encoding='utf8') as f:
 55                     f1 = eval(f.read())
 56                     for i,v in enumerate(f1):
 57                         print(v['name'],v['price'])
 58                     shopping1 = input('是否要继续购物|y|n:').strip()
 59                     if shopping1 == 'y':
 60                         pass
 61                     else:
 62                         exit()
 63                     break
 64             elif shopping_list == 'n':
 65                break
 66             else:
 67                 print('输入错误!')
 68                 continue
 69         else:
 70             print('账号密码错误!')
 71     cont += 1
 72 
 73 if cont == 3:
 74     print('对不起,密码输错三次,账户被锁定!')
 75     f = open('black_file.txt', 'a+')
 76     f.write('%s\n' % name)  # 密码输入三次将账户写入到黑名单文件里
 77     f.close()
 78     exit()
 79 
 80 
 81 
 82 while not exit_flag:
 83     print('-----------商品列表-------')
 84     for i,v in enumerate(goods):
 85         print(i,v['name'],v['price'])
 86 
 87     choice = input('请输入商品的编号或输入q退出!|a添加商品:').strip()
 88 
 89     if choice.isdigit():
 90         choice = int(choice)
 91         if choice >= 0 and choice < len(goods):
 92             product = goods[choice]
 93             print(product['price'])
 94             if  product['price'] <= yi['salary']:
 95                 shopping.append(product)
 96                 yi['salary'] -= product['price']
 97                 with open('b.txt','w',encoding='utf8') as f:
 98                     f.write(str(yi))
 99                 print('商品\033[31;1m%s\033[0m已加入购物车,您的余额还有\033[31;1m%s元\033[0m'%(goods[0]['name'],yi['salary']))
100             else:
101                 print('对不起,您的余额不足,无法购买!')
102             # print(shopping)
103         else:
104             print('商品不存在,请重新输入!')
105 
106     elif choice == 'q' or choice == 'Q':
107         # print(shopping)
108         if len(shopping) > 0:
109             print('-----已购买的商品-------')
110             print('ID 商品 价格')
111             for i,v in enumerate(shopping):
112                 print('%s.  %s %s'%(i,v['name'],v['price']))
113             print('您的余额还剩\033[31;1m%s\033[0m'%yi['salary'])
114             with open('a.txt','w',encoding='utf-8') as f:
115                 f.write(str(shopping))
116             # print(shopping)
117             exit_flag = True
118     elif choice == 'a':
119         print('添加商品')
120         print('已有商品')
121         for i,v in enumerate(goods):
122             print(i,v['name'],v['price'])
123 
124         a = input('商品名称:').strip()
125         a1 = int(input('商品价格:').strip())
126         dic = {'name':a,'price':a1}
127 
128         goods.append(dic)
129 
130         print('商品已添加完成')
131         for i,v in enumerate(goods):
132             print(i,v['name'],v['price'])

 



posted @ 2018-05-09 10:10  游走在边缘的人  阅读(153)  评论(0编辑  收藏  举报