1 #!/usr/bin/env python
  2 import sys
  3 import os
  4 import getpass
  5 
  6 商品={
  7     '图书': {
  8         "《Puppet实战》": 39,
  9         "《ZooKeeper分布式过程协同技术详解》":48,
 10         "《代码审计》":42,
 11         "《白帽子讲浏览器安全》":73,
 12     },
 13     '数码家电': {
 14         "Iphone6S":6000,
 15         "MacBookAir":15000,
 16         "DELL-R720":33000,
 17         "索尼PlayStation VR":2600,},
 18     '食品': {
 19         "茉莉花茶":3,
 20         "康师傅红烧牛肉面":3,
 21         "3+2饼干":5.5,
 22         "红牛":7,
 23     },
 24     '生活用品': {
 25       "汰渍洗衣液-550ml":23,
 26       "清风抽纸:300抽/三层":12,
 27       "电神水壶":98,
 28       "六神花露水":19,
 29     }
 30 }
 31 #定义各项初始值
 32 money='account.txt'
 33 logtime=0
 34 afile='afile.txt'
 35 bfile='bfile.txt'
 36 SHOPING=[]
 37 
 38 #显示余额
 39 def show_money():
 40     with open(money,'r+') as ac:
 41         for line in ac.readlines():
 42             yue = line.strip()
 43             return int(yue)
 44 
 45 
 46 def deny_account(username):
 47     print("This account already locked!")
 48     with open(bfile,'a+') as bf:  # 此处最好为a+模式,本人之前使用a模式错误账号无法写入
 49         bf.write(username + '\n')
 50 flag=False
 51 while logtime < 3:
 52     username = input('\033[31;1m请输入用户名:\033[0m')
 53     with open(bfile, 'r') as lock_f:
 54         # 取出黑名单中的用户名与输入的用户名进行匹配
 55         for line in lock_f.readlines():
 56             if len(line) == 0:
 57                 continue
 58             if username == line.strip():
 59                 sys.exit('\033[32;1m用户 %s 已经被锁定!\033[0m' % username)
 60     # 如果用户名为空重新输入
 61         if len(username) == 0:
 62             print('用户名不能为空,请重新输入')
 63             continue
 64     with open(afile,'r') as af:
 65         for line in af.readlines():
 66             user,pwd = line.strip().split()
 67             if username == user:
 68                 password = getpass._raw_input('请输入用户%s的密码:'%username).strip()
 69                 if username == user and password == pwd:
 70                     print('登陆成功!')
 71                     flag=True
 72                     break
 73                 else:
 74                     if logtime<3:
 75                         print('密码错误请重新输入!')
 76                     logtime+=1
 77                     break
 78         else:
 79             if logtime<3:
 80                 print('用户不存在!')
 81             logtime += 1
 82     if flag==True:
 83         print('Welcome %s come in TG!' % username)
 84         break
 85 else:
 86     deny_account(username)
 87 if flag==True:
 88     while True:
 89         if show_money() < 0:
 90             name1 = "\033[1;31;1m您的账户余额不足,请尽快充值!"
 91             print(name1.center(50, '-'))
 92         else:
 93             print('*'*50)
 94             欢迎信息 = '\033[1;31;1m欢迎登录没有小菊花的网上超市!\033[0m'
 95             print(欢迎信息.center(54, '*'))
 96             print('\033[1;32;1m*\033[1m' * 50)
 97             for lei in 商品:
 98                 print('\033[1;35;1m%s\033[1m'%lei)
 99                # print('\033[1;35;1m%s\033[1m'%lei)
100                 print('\033[1;32;1m*\033[1m'*50)
101                 选择类 = input('\033[1;34;1m请选择类目:\033[0m')
102         #二级流程
103                 for s in 商品[选择类]:
104                     print('\033[1;32;1m-\033[1m' * 50)
105                     print(s)
106                     选择商品=input('请选择商品:')
107                     n = input('输入数量:')
108                     jiage=商品[选择类][选择商品]
109                     print('\033[1;31;1m本商品单价为:%d¥,本次共计添加%d个,总价为%d*%d=%d¥\033[0m'%(jiage,int(n),jiage,int(n),jiage*int(n)))
110                     SHOPING.extend([jiage*int(n)])
111                     print('\033[1;32;1m-\033[1m' * 50)
112                     xuanze=input('\033[1;31;1m是否继续购物,Y键返回主菜单,N进入购物车结算,请选择!\033[0m')
113             #进入购物车结算
114                     if xuanze =='N':
115                         print('%s'%SHOPING)
116                         结算=input('是否结算?Y/N')
117                         if 结算 =='Y':
118                             余额=show_money()-int(sum(SHOPING))
119                             print(余额)
120                             with open(money, 'w+') as ac:
121                                 ac.writelines('%s'%余额)
122                             #break
123                         继续=input('是否继续购物(Y/N)?退出:N,返回上一级:Y.')
124                         if 继续=='Y':
125                             sys.exc_traceback()
126                         else:
127                             sys.exit()
128                     elif xuanze=='Y':
129                         break
130                 else:
131                     print("您选择的商品不存在!请重新选择!")
132                     break
133             else:
134                 print("选择的类目不存在!")
135                 continue
136             break
137         break

 afile.txt内容

carl openstack

helen tianle

lucy hr

kevin usa

bfile.txt内容随意

posted on 2016-04-18 15:44  geek3  阅读(351)  评论(0编辑  收藏  举报