1 count = 0 2 user_info = {'yileye': 123, 'root': 123} 3 4 user_msg = ''' 5 --------------------User info----------------- 6 user_name: %s 7 8 check_status: %s 9 -------------------------------------------- 10 ''' 11 12 with open('user_info.log', 'r') as rf: 13 lock_user = rf.read() 14 while True: 15 user_name = input('user_name: ') 16 if user_name == lock_user: 17 print('账号被锁定') 18 break 19 if user_name not in user_info: 20 print('无效用户名,请重试') 21 continue 22 23 if count == 3: 24 print('超出重试次数,账号被锁定') 25 with open('user_info.log', 'w') as f: 26 f.write(user_name) 27 break 28 user_pwd = input('user_pwd: ') 29 if int(user_pwd) != user_info[user_name]: 30 print('密码错误请重试') 31 count += 1 32 continue 33 print(user_msg % (user_name, '登录成功')) 34 break
35 36 # ------------------------------------------------- 37 38 # 猜龄游戏 39 40 age = 26 41 42 age_scope = 5 43 44 count = 0 45 46 flag = True 47 48 while True: 49 50 while flag: 51 52 while flag: 53 try: 54 guess_age = int(input('猜猜我的年龄?\n')) 55 except ValueError: 56 print('请输入有效年龄') 57 flag = False 58 break 59 count += 1 60 if guess_age == age: 61 if count == 1: 62 print('真厉害,1次就猜对了') 63 flag = False 64 break 65 else: 66 print('不怎么样嘛,' + str(count) + '次才猜对') 67 flag = False 68 count = 0 69 break 70 elif abs(age - guess_age) <= age_scope: 71 print('很接近了,加油') 72 73 print('还可以猜' + str(3 - count) + '次') 74 if count == 3: 75 flag = False 76 if count == 3: 77 continue_or_quit = input('是否还想继续玩? y|n \n') 78 if continue_or_quit.lower() == 'y': 79 count = 0 80 flag = True 81 continue 82 else: 83 break 84 break