标签:utf-8 else 错误 col 密码 exception cas inpu case
1 # -*- coding: utf-8 -*- 2 ‘‘‘ 3 Author:SL 4 ‘‘‘ 5 import os,time 6 7 #开始登录界面 8 def login(): 9 welcome =‘‘‘ 10 ----欢迎使用自动取款机系统----- 11 操作指南: 12 【1】用户登录 13 【2】管理员登录 14 【3】退出系统 15 ‘‘‘ 16 while True: 17 print(welcome) 18 choice_num =input(‘请输入指令序号:‘).strip() 19 if choice_num.isdigit() and choice_num in [‘1‘,‘2‘,‘3‘]: 20 return choice_num 21 else: 22 print(‘您的输入指令有误,请重新选择!‘) 23 continue 24 25 #用户登录 26 def user_login(): 27 while 1: 28 user_name = input(‘请输入用户名:\n>>>‘).strip() 29 user_pwd = input(‘请输入密码:\n>>>‘).strip() 30 with open(‘user_locked.txt‘,‘r‘,encoding=‘utf8‘) as f1: 31 for line in f1: 32 if user_name in line: 33 print(‘该账户处于冻结状态,请联系管理员!‘) 34 return 0 35 with open(‘user_info.txt‘,‘r‘,encoding=‘utf8‘) as f2: 36 for line in f2: 37 try: 38 line_list =line.split(‘:‘) 39 if line_list[1].startswith(user_name) and line_list[2].startswith(user_pwd):#验证用户名和密码 40 print(‘用户登录成功!‘) 41 return user_name 42 else: 43 print(‘账户名或密码错误,请重新输入!‘) 44 continue 45 except Exception: 46 print(‘账户名或密码错误,请重新输入!‘) 47 48 #管理员登录 49 def root_login(): 50 ‘‘‘ 51 系统默认管理员名字:root 密码:123 52 ‘‘‘ 53 while True: 54 user_name = input(‘请输入管理员用户名,默认root【Q退出】:\n>>>‘).strip() 55 if user_name.lower() == ‘q‘: 56 return 0 #退出返回0 57 password = input(‘请输入管理员密码,默认123:\n>>>‘).strip() 58 if user_name.lower() == ‘root‘ and password == ‘123‘: 59 print(‘您已获得管理者权限!‘) 60 return 1 #成功返回1 61 else: 62 print(‘请重新输入!‘) 63 continue 64 65 #用户登录操作说明 66 def user_welcome(): 67 user_welcome =‘‘‘ 68 操作说明: 69 【取款】:1 【余额查询】:4 70 【存款】:2 【定期还款】:5 71 【转账】:3 【显示账单】:6 72 【返回】:R 73 ‘‘‘ 74 print(user_welcome) 75 76 #管理员操作说明 77 def root_welcome(): 78 root_welcome =‘‘‘ 79 操作说明: 80 【添加用户】:1 81 【冻结用户】:2 82 【设定额度】:3 83 【返回】:R 84 ‘‘‘ 85 print(root_welcome) 86 87 #取款操作 88 def withdrawal(user_name): 89 while True: 90 get_money =input("请输入取款数额【返回R】:\n>>>").strip() 91 if get_money.lower() ==‘r‘: 92 break 93 elif get_money.isdigit(): 94 f1 = open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) 95 f2 = open(‘new_user_info.txt‘, ‘w‘, encoding=‘utf8‘) 96 f1.seek(0) 97 for line in f1: 98 if user_name in line: 99 if int(line.split(‘:‘)[3][:-5]) >int(get_money): 100 balance =int(line.split(‘:‘)[3][:-5])-int(get_money) 101 line =line.replace(str(line.split(‘:‘)[3][:-5]),str(balance)) 102 print(‘取款成功!余额为%s‘%balance) 103 else: 104 print(‘余额不足!‘) 105 f2.write(line) 106 f1.close() 107 f2.close() 108 os.remove(‘user_info.txt‘) 109 os.rename(‘new_user_info.txt‘,‘user_info.txt‘) 110 else: 111 print(‘请输入正确的金额!‘) 112 113 #存款操作 114 def deposit(user_name): 115 while True: 116 give_money =input("请输入存款数额【返回R】:").strip() 117 if give_money.lower() ==‘r‘: 118 break 119 elif give_money.isdigit(): 120 f1 = open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) 121 f2 = open(‘new_user_info.txt‘, ‘w‘, encoding=‘utf8‘) 122 f1.seek(0) 123 for line in f1: 124 if user_name in line: 125 balance =int(line.split(‘:‘)[3][:-5])+int(give_money) 126 line =line.replace(str(line.split(‘:‘)[3][:-5]),str(balance)) 127 print(‘存款成功!余额为%s‘%balance) 128 f2.write(line) 129 f1.close() 130 f2.close() 131 os.remove(‘user_info.txt‘) 132 os.rename(‘new_user_info.txt‘,‘user_info.txt‘) 133 else: 134 print(‘请输入正确的金额!‘) 135 136 #转账操作 137 def trans_account(user_name): 138 while True: 139 flag = False 140 trans_name =input(‘请输入收款人账户名【返回R】:\n>>>‘).strip() 141 if trans_name.lower() ==‘r‘: 142 break 143 with open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) as f1: 144 for line in f1: 145 if trans_name in line and trans_name != user_name: 146 flag =True 147 if flag: 148 while True: 149 trans_money = input("请输入转账数额【重选收款人R】:\n>>>").strip() 150 if trans_money.lower() == ‘r‘: 151 break 152 elif trans_money.isdigit(): 153 msg_insure =input(‘【确定Y】【取消N】>>>‘) 154 if msg_insure.lower() ==‘y‘: 155 f1 = open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) 156 f2 = open(‘new_user_info.txt‘, ‘w‘, encoding=‘utf8‘) 157 for line in f1: 158 if user_name in line: 159 balance = int(line.split(‘:‘)[3][:-5]) - int(trans_money) 160 line = line.replace(str(line.split(‘:‘)[3][:-5]), str(balance)) 161 if trans_name in line: 162 balance = int(line.split(‘:‘)[3][:-5]) + int(trans_money) 163 line = line.replace(str(line.split(‘:‘)[3][:-5]), str(balance)) 164 f2.write(line) 165 f1.close() 166 f2.close() 167 os.remove(‘user_info.txt‘) 168 os.rename(‘new_user_info.txt‘, ‘user_info.txt‘) 169 print(‘转账成功!‘) 170 elif msg_insure.lower() ==‘n‘: 171 continue 172 else: 173 print(‘您的操作有误,请重新选择收款人!‘) 174 break 175 else: 176 print(‘请输入正确的金额!‘) 177 else: 178 print(‘收款人不存在,请重新输入!‘) 179 180 #余额查询 181 def balance_query(user_name): 182 with open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) as f: 183 for line in f: 184 if user_name in line: 185 balance = int(line.split(‘:‘)[3][:-5]) 186 print(‘您的余额为%s!‘%balance) 187 188 #定期还款 189 def reg_payment(user_name): 190 while True: 191 with open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) as f: 192 for line in f: 193 if user_name in line: 194 debt = int(line.split(‘:‘)[-1]) 195 print(‘您的欠款为%s!‘ % debt) 196 set_money =input(‘请输入您的每次分期还款金额【返回R】:\n>>>‘).strip() 197 if set_money.lower() ==‘r‘: 198 break 199 set_freq =input(‘请输入您的分期还款次数(分期金额*次数应小于欠款数额,否则后果自负!):\n>>>‘).strip() 200 count = 0 201 if set_money.isdigit() and set_freq.isdigit(): 202 for i in range(int(set_freq)): 203 f1 = open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) 204 f2 = open(‘new_user_info.txt‘, ‘w‘, encoding=‘utf8‘) 205 for line in f1: 206 if user_name in line: 207 debt = int(line.split(‘:‘)[-1]) - int(set_money) 208 line = line.replace(str(line.split(‘:‘)[-1]), str(debt)+‘\n‘) 209 balance = int(line.split(‘:‘)[3][:-5]) - int(set_money) 210 line = line.replace(str(line.split(‘:‘)[3][:-5]), str(balance)) 211 f2.write(line) 212 f1.close() 213 f2.close() 214 os.remove(‘user_info.txt‘) 215 os.rename(‘new_user_info.txt‘, ‘user_info.txt‘) 216 count +=1 217 time.sleep(5) # 设定分期还款间隔时间,这里默认每隔5秒还款一次 218 print(‘已经还款%s期!‘%count) 219 print(‘分期还款完成!‘) 220 with open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) as f: 221 for line in f: 222 if user_name in line: 223 debt = int(line.split(‘:‘)[-1]) 224 print(‘您剩余欠款为%s!‘ % debt) 225 return 226 else: 227 print(‘请重新输入!‘) 228 229 #显示账单 230 def debt_query(user_name): 231 with open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) as f: 232 for line in f: 233 if user_name in line: 234 balance = int(line.split(‘:‘)[3][:-5]) 235 debt = int(line.split(‘:‘)[-1]) 236 print(‘您的账户信息为余额:%s,欠款:%s!‘%(balance,debt)) 237 238 #添加用户 239 def add_user(): 240 while True: 241 add_flag =True 242 new_user_name =input(‘请输入注册账户名【返回R】:\n>>>‘).strip() 243 if new_user_name.lower() == ‘r‘: 244 break 245 new_user_pwd =input(‘请输入密码【返回R】:‘).strip() 246 if new_user_pwd.lower() ==‘r‘: 247 break 248 else: 249 f1 =open(‘user_info.txt‘, ‘r+‘, encoding=‘utf8‘) 250 f2 =open(‘user_locked.txt‘, ‘r‘, encoding=‘utf8‘) 251 for line in f2: 252 if new_user_name in line: 253 print(‘该账户已经被冻结,请重新输入账户名!‘) 254 add_flag =False 255 if add_flag: 256 for line1 in f1: 257 if new_user_name in line1: 258 print(‘该账户已经存在,请重新输入账户名!‘) 259 break 260 else: 261 f1.write(‘\nname:‘+new_user_name+‘,password:‘+new_user_pwd+‘,balance:10000,debt:0‘) 262 print(‘新用户注册成功!‘) 263 f1.close() 264 f2.close() 265 266 #冻结用户 267 def frozen_user(): 268 while 1: 269 frozen_flag = False 270 lock_flag = True 271 set_frozen_user = input(‘请输入要冻结的账户名【返回R】:‘).strip() 272 if set_frozen_user.lower() == ‘r‘: 273 break 274 with open(‘user_info.txt‘, ‘r‘, encoding=‘utf8‘) as f: 275 for line in f: 276 if set_frozen_user in line: 277 frozen_flag = True 278 if frozen_flag: 279 with open(‘user_locked.txt‘, ‘r+‘, encoding=‘utf8‘) as f1: 280 for line in f1: 281 if set_frozen_user in line: 282 print(‘该账户已经处于冻结状态,无需再次冻结!‘) 283 lock_flag = False 284 if lock_flag: 285 f1.write(set_frozen_user + ‘\n‘) 286 print(‘成功冻结该账户!‘) 287 else: 288 print(‘对不起,该用户不存在!‘) 289 290 #设定用户额度 291 def set_account(): 292 pass 293 294 #用户操作选择 295 def user_switch(user_name): 296 while 1: 297 user_welcome() 298 case = input(‘请输入要进行操作的代号:‘).strip() 299 if case == ‘1‘: 300 withdrawal(user_name) 301 elif case == ‘2‘: 302 deposit(user_name) 303 elif case == ‘3‘: 304 trans_account(user_name) 305 elif case == ‘4‘: 306 balance_query(user_name) 307 elif case == ‘5‘: 308 reg_payment(user_name) 309 elif case == ‘6‘: 310 debt_query(user_name) 311 elif case.lower() == ‘r‘: 312 break 313 else: 314 print(‘您的输入有误,请重新选择!‘) 315 316 #管理员操作选择 317 def root_switch(): 318 while 1: 319 root_welcome() 320 case = input(‘请输入要进行操作的代号:‘).strip() 321 if case == ‘1‘: 322 add_user() 323 elif case == ‘2‘: 324 frozen_user() 325 elif case == ‘3‘: 326 set_account() 327 elif case.lower() == ‘r‘: 328 break 329 else: 330 print(‘您的操作有误,请重新选择!‘) 331 332 #主程序入口 333 if __name__ == ‘__main__‘: 334 while True: 335 choice_num = login() 336 if choice_num == ‘3‘: 337 print(‘退出成功,感谢您的使用!‘) 338 break 339 if choice_num == ‘1‘: 340 res = user_login() 341 if res == 0: 342 continue 343 else: 344 user_switch(res) 345 if choice_num == ‘2‘: 346 res = root_login() 347 if res == 0: 348 continue 349 if res == 1: 350 root_switch()
标签:utf-8 else 错误 col 密码 exception cas inpu case
原文地址:http://www.cnblogs.com/sl-swift/p/7851680.html