python代码输错怎么修改_Python小练习更改版(更改一部分代码,与错误)

1 #!/usr/bin/env python

2 #coding:utf-8

3 importsys4 classShop(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 =012 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 inself.lock_name:21 return 0 #如果名字在user_file里面,且没在锁定文件里面的话,返回0

22 elif a in self.user_list and a inself.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 return033 else:34 count -= 1

35 print self.Red,"密码错误,剩余%d次" %count,self.Res36 b = raw_input("请再次输入你的密码:") ######更改部分给用户机会输入密码

37 exceptKeyError :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 returnself.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.Res54 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 whileTrue: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.Res75 money = raw_input("输入账号资金:")76 ifmoney.isdigit():77 print self.Red,"""name: %s78 passwd: %s79 money: %d""" %(sign_name,passwd,int(money)),self.Res80 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.Res92 continue

93 else:94 print self.Red,"资金错误,返回重来",self.Res95 continue

96 else:97 print self.Red,"账号不可用",self.Res98 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 inself.shop_list[i].items():109 printkey,value110

111

112 def shoping(self,b): ##########购买函数

113 whileTrue:114 prompts = self.Gre+"请选择你要的商品编号(q|Q,退出结算):"+self.Res115 index =raw_input(prompts)116 if index.upper() == "Q":117 print self.Red,"购物结束,去结算!",self.Res118 printb119 self.money_list[name] = b #####结算部分

120 printself.money_list121 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 ifindex.isdigit():127 index =int(index)128 b =self.shopcar(index,b)129 else:130 print self.Red,"你选择的商品不存在,请确认你的选择!!!",self.Res131 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.Res139 self.cars.append(shop_money)140 b -=shop_money141 self.customer_expense +=shop_money142 printself.cars143 returnb144 else:145 print self.Red, "账户余额可能不足哦,还剩%d" %b, self.Res146 returnb147 exceptException,e:148 print self.Red,"你选择的商品不存在!",self.Res149 returnb150

151

152

153

154

155 maomao =Shop()156

157 if __name__ == "__main__":158 whileTrue: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 ifstr(d).isdigit() :169 maomao.shoping(d) ####处理用户购物动作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值