新手写作,用来练习与提高python编写、思考能力,有错误的地方请指正,谢谢!
第一次写博客,课题是一位大神的博客,本着练习的目的,就自己重写了一遍,有很多不足的地方,希望借博客记录下自己的成长;
#!/usr/bin/env python#coding:utf-8import sysclass Shop(object): def __init__(self): self.Red = "\033[1;31m" self.Gre = "\033[1;32m" self.Res = "\033[0m" with open('user_file.txt','r') as fp: user_list = eval(fp.readline()) # 用户的名字与密码转换成字典 with open('lock_name.txt','r') as fp2: lock_name = eval(fp2.readline()) # 锁定的用户为列表 with open('money_list.txt','r') as fp3: money_list = eval(fp3.readline()) def nameCheck(self,a): if a in self.user_list and a not in self.lock_name: return 0 #如果名字在user_file里面,且没在锁定文件里面的话,返回0 elif a in self.user_list and a in self.lock_name: return 1 #名字在user_file里面,且被锁定,返回1 else: return 2 #否则返回2 def passwdCheck(self,a,b): count = 3 while count > 1: try: if b == self.user_list[a]: return 0 else: count -= 1 print "密码错误,剩余%d次" % count b = raw_input("请再次输入你的密码: ") except KeyError : return 2 else: return 1 def checkAfter(self,b,c,dd): if b == 0 and c == 0 : print self.Gre,"恭喜你(%s),登陆成功!!!,欢迎你来到我们的商店!"%dd,self.Res #成功后动作进到商店 self.shopCounts() elif b == 1 : print self.Red,"对不起,你(%s)的账号被锁定,请联系客服!!!"%dd,self.Res #不成功则动作回到初始位置 pass elif c == 1 : print "密码错误,账号将被锁定" self.nameLock(dd) else: print self.Red,"账号(%s)不存在,是否创建用户"%dd,self.Res #不存在则动作到注册位置 self.signName() def shopCounts(self): shop_list = { "phone":2000,"ipad":1500,"ice_box":500} for key,value in shop_list.items(): print key," : ",value #打印商店内容 def signName(self): while True: sign_name = raw_input("请输入你要注册的账号:") value = self.nameCheck(sign_name) passwd = raw_input("输入你的密码: ") if value == 2: print "congratulations,账号可用" money = raw_input("输入账号资金: ") if money.isdigit(): print """name: %spasswd: %smoney: %d""" %(sign_name,passwd,int(money)) self.user_list.setdefault(sign_name,passwd) self.money_list.setdefault(sign_name,money) confirm = raw_input("是否确认以上信息: ") if confirm.upper() == "Y": with open('user_file.txt','r+') as fp: fp.write(str(self.user_list)) with open('money_list.txt','r+') as fp2: fp2.write(str(self.money_list)) break else: print "重新来过" continue else: print "资金错误,返回重来" continue else: print "账号不可用" continue def nameLock(self,a): self.lock_name.append(a) with open('lock_name.txt','r+') as fp: fp.write(str(self.lock_name))maomao = Shop()while True: name = raw_input("请输入你的账号:") if name.upper() == "Q": sys.exit() else: passwd = raw_input("请输入你的密码:") b = maomao.nameCheck(name) c = maomao.passwdCheck(name,passwd) maomao.checkAfter(b,c,name)