标签:pts box odi break sys dex except 检测 lines
之前上传的发现有部分代码错误,重新上传;
更改了第一次的代码与错误,增加了注释与商店部分功能;
没有每天坚持更新博客,与初衷相差甚远,坚持!每天进步一点点!
user_list.txt 部分代码:
{‘test1‘: ‘test1‘, ‘test3‘: ‘test3‘, ‘test2‘: ‘test2‘}
lock_list.txt 部分代码:
[‘test1‘]
money_list.txt 部分代码:
{‘test1‘: 2000, ‘test3‘: 3000, ‘test2‘: 500 }
1 #!/usr/bin/env python 2 #coding:utf-8 3 import sys 4 class Shop(object): 5 def __init__(self): 6 self.Red = "\033[1;31m" 7 self.Gre = "\033[1;32m" 8 self.Res = "\033[0m" 9 self.shop_list = [{"1:phone": 2000}, {"2:ipad": 1500}, {"3:ice_box": 500}] 10 self.cars = [] 11 self.customer_expense = 0 12 with open(‘user_file.txt‘,‘r‘) as fp: 13 user_list = eval(fp.readline()) # 用户的名字与密码转换成字典 14 with open(‘lock_name.txt‘,‘r‘) as fp2: 15 lock_name = eval(fp2.readline()) # 锁定的用户为列表 16 with open(‘money_list.txt‘,‘r‘) as fp3: 17 money_list = eval(fp3.readline()) 18 19 def nameCheck(self,a): #########名字检测状态函数 20 if a in self.user_list and a not in self.lock_name: 21 return 0 #如果名字在user_file里面,且没在锁定文件里面的话,返回0 22 elif a in self.user_list and a in self.lock_name: 23 return 1 #名字在user_file里面,且被锁定,返回1 24 else: 25 return 2 #否则返回2 26 27 def passwdCheck(self,a,b): ##########密码检测函数 28 count = 3 29 while count > 1: 30 try: 31 if b == self.user_list[a]: 32 return 0 33 else: 34 count -= 1 35 print self.Red,"密码错误,剩余%d次" % count,self.Res 36 b = raw_input("请再次输入你的密码: ") ######更改部分给用户机会输入密码 37 except KeyError : 38 return 2 39 else: 40 return 1 41 42 43 def checkAfter(self,b,c,dd): ##########状态检测完成后的动作 44 if b == 0 and c == 0 : 45 print self.Gre,"恭喜你(%s),登陆成功!!!,欢迎你来到我们的商店!"%dd,self.Res #成功后动作进到商店 46 self.shopCounts() 47 return self.money_list[dd] 48 49 elif b == 1 : 50 print self.Red,"对不起,你(%s)的账号被锁定,请联系客服!!!"%dd,self.Res #不成功则动作回到初始位置 51 return ‘error‘ 52 elif c == 1 : 53 print self.Red,"密码错误,账号将被锁定",self.Res 54 self.nameLock(dd) 55 return ‘right‘ 56 else: 57 prompts = self.Red+"账号(%s)不存在,是否创建用户(q|Q退出): "%dd+self.Res#不存在则动作到注册位置 58 confirm = raw_input(prompts) 59 if confirm.upper() == "Q": #######增加退出接口 60 return ‘error‘ 61 else: 62 self.signName() 63 return ‘right‘ 64 65 66 def signName(self): #########注册账号函数 67 while True: 68 sign_name = raw_input("请输入你要注册的账号(q|Q退出!):") 69 value = self.nameCheck(sign_name) 70 if sign_name.upper() == "Q": #####增加退出接口 71 break 72 passwd = raw_input("输入你的密码: ") 73 if value == 2: 74 print self.Gre,"congratulations,账号可用",self.Res 75 money = raw_input("输入账号资金: ") 76 if money.isdigit(): 77 print self.Red,"""name: %s 78 passwd: %s 79 money: %d""" %(sign_name,passwd,int(money)),self.Res 80 confirm = raw_input("是否确认以上信息(y|Y确认,其它返回重来): ") 81 if confirm.upper() == "Y": 82 self.user_list.setdefault(sign_name, passwd) #######更改部分,用户不确认也会将信息存入文件 83 self.money_list.setdefault(sign_name, int(money)) ####### 84 with open(‘user_file.txt‘,‘r+‘) as fp: 85 fp.write(str(self.user_list)) 86 with open(‘money_list.txt‘,‘r+‘) as fp3: 87 fp3.write(str(self.money_list)) 88 break 89 90 else: 91 print self.Red,"重新来过",self.Res 92 continue 93 else: 94 print self.Red,"资金错误,返回重来",self.Res 95 continue 96 else: 97 print self.Red,"账号不可用",self.Res 98 continue 99 def nameLock(self,a): ##########锁定账号函数 100 self.lock_name.append(a) 101 with open(‘lock_name.txt‘,‘r+‘) as fp2: 102 fp2.write(str(self.lock_name)) 103 104 def shopCounts(self): #########商店部分函数 105 # for key,value in shop_list.items(): 106 # print key," : ",value #打印商店内容 107 for i in range(3): 108 for key,value in self.shop_list[i].items(): 109 print key,value 110 111 112 def shoping(self,b): ##########购买函数 113 while True: 114 prompts = self.Gre+"请选择你要的商品编号(q|Q,退出结算): "+self.Res 115 index = raw_input(prompts) 116 if index.upper() == "Q": 117 print self.Red,"购物结束,去结算!",self.Res 118 print b 119 self.money_list[name] = b #####结算部分 120 print self.money_list 121 with open(‘money_list.txt‘,‘w‘) as fp3: ########此处若是r+ 模式则会出现编码,暂时没弄明白!!! 122 fp3.writelines(str(self.money_list)) 123 print "#"*20 124 # print fp3.read() 125 break 126 if index.isdigit(): 127 index = int(index) 128 b = self.shopcar(index,b) 129 else: 130 print self.Red,"你选择的商品不存在,请确认你的选择!!!",self.Res 131 self.shopCounts() 132 133 def shopcar(self,a,b): ####购物车函数 134 try: 135 shop_money = self.shop_list[(a-1)].values()[0] 136 # print b 137 if b >= shop_money: 138 print self.Gre, "congratulations!,商品已加入购物车", self.Res 139 self.cars.append(shop_money) 140 b -= shop_money 141 self.customer_expense += shop_money 142 print self.cars 143 return b 144 else: 145 print self.Red, "账户余额可能不足哦,还剩%d" % b, self.Res 146 return b 147 except Exception,e: 148 print self.Red,"你选择的商品不存在!",self.Res 149 return b 150 151 152 153 154 155 maomao = Shop() 156 157 if __name__ == "__main__": 158 while True: 159 name = raw_input("请输入你的账号(q|Q退出):") 160 161 if name.upper() == "Q": 162 sys.exit() 163 else: 164 passwd = raw_input("请输入你的密码:") 165 b = maomao.nameCheck(name) ######接收用户检测状态值 166 c = maomao.passwdCheck(name,passwd) #####接收用户密码检测状态值 167 d = maomao.checkAfter(b,c,name) ########接收用户的moneye值 168 if str(d).isdigit() : 169 maomao.shoping(d) ####处理用户购物动作
标签:pts box odi break sys dex except 检测 lines
原文地址:http://www.cnblogs.com/Mail-maomao/p/6886471.html