购物车程序新版

需求如下:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资
,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,
需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消
费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # @Time    : 2017/9/17 13:02
  4 # @Author  : lichuan
  5 # @File    : file_test.py
  6 
  7 shop_list={
  8     'apple' : 7888,
  9     'xiaomi' : 2000,
 10     'huawei' : 2500,
 11     'meizu' : 1800
 12 }
 13 
 14 FLAG_TAG=True
 15 
 16 
 17 while FLAG_TAG:
 18     user_name=input("please input your username:").strip()
 19     passwd=input("please input your passwd:").strip()
 20 
 21     with open('a.txt','r',encoding='utf-8') as a_read,open('a.txt','r+',encoding='utf-8') as a_write:
 22         for i in a_read:
 23             # print("---------------%d" %len(i)+"---------------")
 24             #continue
 25             # userinfo = {}
 26             if len(i)==1:
 27                 #print(123)
 28                 continue
 29             i=eval(i)
 30             buy_list = {}
 31             ago_buy_list=i["buy_list"]
 32 
 33             if i["username"] == user_name and i['passwd'] == passwd:
 34                 if len(i['buy_list']) != 0:
 35                     print("\033[1m 上次消费记录: %s " % str(i["buy_list"]) +'\033[0m' )
 36                     print("\033[1m  salary: %d" % i['salary'] + '\033[0m')
 37                 while True:
 38                     if i['salary'] == 0 :
 39                         s=input("please input your salary:").strip()
 40                         #如果不为数字或者数字为0,则重新输入
 41                         if (not s.isdigit()) or int(s) == 0:
 42                             print("wrong input,try again!")
 43                             continue
 44 
 45                         i['salary']=int(s)
 46                         FLAG_TAG=False
 47 
 48                     #da==打印商品列表
 49                     count=0
 50                     buy=[]
 51                     for s in shop_list:
 52                         print("%d,%s %d" %(count,s,shop_list[s]))
 53                         buy.append(s)
 54                         count+=1
 55                     p=input("q==exit,please input your number:").strip()
 56 
 57                     if p == 'q':
 58                         FLAG_TAG=False
 59                         print("\033[1m  salary: %d" % i['salary'] + '\033[0m')
 60                         print("\033[1m  已购买商品: %s " % str(buy_list) + '\033[0m')
 61                         break
 62                     elif p.isdigit() and int(p) < count:
 63                         p=int(p)
 64                         if buy[p] in buy_list and i['salary'] >= shop_list[buy[p]]:
 65                             buy_list[buy[p]]+=1
 66                             i['salary'] = i['salary'] - shop_list[buy[p]]
 67                             print("\033[1m  salary: %d" % i['salary']+'\033[0m')
 68                             print("\033[1m  %s 已加入购物车" % buy[p]+'\033[0m')
 69                             continue
 70                         elif i['salary'] >= shop_list[buy[p]] :
 71                             buy_list[buy[p]]=1
 72                             i['salary']=i['salary']-shop_list[buy[p]]
 73                             print("\033[1m  salary: %d" % i['salary']+'\033[0m')
 74                             print("\033[1m  %s 已加入购物车" % buy[p] + '\033[0m')
 75                         else:
 76                             print("余额不足,请选择别的商品!")
 77                             print("\033[1m  salary: %d" % i['salary'] +'\033[0m')
 78                             continue
 79                     else:
 80                         print("wrong input , try again!")
 81                         continue
 82 
 83                 if len(buy_list) != 0:
 84                     print(ago_buy_list)
 85                     ago_buy_list=buy_list
 86             i['buy_list'] = ago_buy_list
 87             print(str(i))
 88             a_write.write(r'%s' % str(i)+'\n')
 89             a_write.flush() 
 90 
 91         #根据输入判断,用户密码错误且输入不是‘q'或者’g'的,重新循环,输入用户名密码
 92         if FLAG_TAG:
 93             print("username or passwd is wrong,try again please!")
 94             c=input("q==exit,g=continue:").strip()
 95             if c == 'q':
 96                 FLAG_TAG=False
 97                 break
 98             elif c == 'g':
 99                 continue
100 
101     FLAG_TAG=False
原文地址:https://www.cnblogs.com/litzhiai/p/7553260.html