Python s12 Day1 笔记及作业 python登录实现 python三级菜单
作业一:
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
python3代码 :
# -*- coding:utf-8 -*- input_name = input("Please input your user name : ").strip() user_lockfile = open("user_lockfile.txt","r+") user_file = open("user_file.txt") user_list = user_file.readlines() for i in range(3): input_passwd = input("Please input your password : ").strip() #查找被锁用户列表判断是否被锁住 if input_name in [locked_user.rstrip() for locked_user in user_lockfile.readlines()]: print("Sorry, your account is locked!") user_file.close() user_lockfile.close() exit(1) else: #没有被锁住,查找用户列表 if input_name not in [user_record.split()[0] for user_record in user_list]: print("Sorry, your account doesn't exist!") user_file.close() user_lockfile.close() exit(2) else: #用户存在,判断密码是否正确,正确跳出循环 input_record = input_name + ' ' + input_passwd if input_record in [user_record.rstrip() for user_record in user_list]: print("Logging in...") user_file.close() user_lockfile.close() exit(0) else:#密码不正确,判断错误次数,错误三次(i=2)将用户锁住 if i == 2: user_lockfile.write(input_name + "\n") print("Sorry, you're locked!") user_file.close() user_lockfile.close() exit(3) else:#剩余机会数 2-i chance = 2 - i print("Wrong password! %s chances left!" % chance)
userfile:
wayne 123
root shroot123
user_lockfile:
kevin
james
root
作业二:
- 三级菜单
- 可以此选择进入各子菜单
- 所需知识点:列表、字典
python3代码:
1 # -*- coding:utf-8 -*- 2 import time 3 import os 4 5 DICT_PRO = {"北京":["大兴区","朝阳区","海淀区","东城区","西城区","丰台区","通州区"], 6 "上海":["静安区","徐汇区","浦东新区","虹口区","普陀区","长宁区","宝山区","嘉定区","闵行区"], 7 "山东省":["烟台市","济南市","青岛市"], 8 "辽宁省":["大连市","沈阳市"], 9 "香港":None, 10 "澳门":None, 11 "台湾":["台北市"]} 12 DICT_CIT = {"烟台市":["莱山区","芝罘区","福山区","龙口市"], 13 "青岛市":["李沧区","台东区","崂山区"], 14 "济南市":["市中区","天桥区","历城区"], 15 "大连市":["中山区","甘井子区","沙河口区","高新园区"], 16 "沈阳市":["皇姑区","大东区","和平区","铁西区"]} 17 18 #将输入的数字转化成整数形式 19 def input_verify(choice): 20 if str.isdigit(choice): 21 choice = int(choice) 22 return choice 23 24 #输出框架 25 def framework_show(province='', city='', district=''): 26 os.system('cls') #清屏 27 print(''' 28 ###################################################### 29 * * 30 * 欢迎进入省市区查询系统 * 31 * * 32 ###################################################### 33 * * 34 * 省份 : %s 城市 : %s 区 : %s 35 * * 36 ###################################################### 37 ''' % (province, city, district)) 38 39 #展示欢迎界面 40 def welcome_show(province='', city='', district=''): 41 print(''' 42 ###################################################### 43 * * 44 * Welcome to %s %s %s 45 * * 46 ###################################################### 47 ''' % (province, city, district)) 48 49 #从省份字典,提取省份以及直辖市展示 50 def province_show(): 51 global DICT_PRO 52 global P_NAME 53 global C_NAME 54 global D_NAME 55 global C_FLAG 56 57 province_dict = {} 58 #遍历省份字典,提取省份并添加编号输出展示 59 for (n, p) in enumerate(DICT_PRO, 1): 60 province_dict[n] = p 61 print('%d.%s' % (n, p) + '\t', end='') 62 if(n % 4 == 0): 63 print() 64 print('\n================================================================') 65 print('q : Exit') 66 province_input = input('请输入省份编号或省份名称 : ') 67 province_input = input_verify(province_input) 68 if province_input == 'q': 69 exit(0) 70 elif province_input in province_dict.keys(): #输入的是数字编号,对全局省份赋值 71 P_NAME = province_dict[province_input] 72 elif province_input in province_dict.values():#输入的是省份名称 73 P_NAME = province_input 74 else: 75 P_NAME = '' #其他输入,提示输入错误 76 print("Wrong Input!") 77 time.sleep(2) 78 79 while P_NAME: 80 framework_show(P_NAME, C_NAME, D_NAME) #调用框架 81 if type(DICT_PRO[P_NAME]) is list: #若省份后有城市列表,调用城市展示函数 82 city_show(P_NAME) 83 if C_FLAG == 'b': 84 break 85 else: #若省份后无城市,直接调用展示函数 86 welcome_show(P_NAME) 87 time.sleep(5) 88 P_NAME = '' 89 break 90 91 92 #传入省份,展示城市列表 93 def city_show(province): 94 global DICT_PRO 95 global DICT_CIT 96 global P_NAME 97 global C_NAME 98 global D_NAME 99 global C_FLAG 100 global D_FLAG 101 102 city_list = DICT_PRO[province] 103 city_dict = {} 104 for (n, c) in enumerate(city_list, 1): 105 city_dict[n] = c 106 print('%d.%s' % (n, c) + '\t', end='') 107 if (n % 4 == 0): 108 print() 109 print('\n================================================================') 110 print('q : Exit b : Back') 111 city_input = input('请输入城市编号或城市名称:') 112 city_input = input_verify(city_input) 113 114 if city_input == 'q': 115 exit(0) 116 elif city_input == 'b': 117 (P_NAME, C_NAME, C_FLAG) = ('', '', 'b') 118 return 119 elif city_input in city_dict.keys(): 120 city_name = city_dict[city_input] 121 (P_NAME, C_NAME, C_FLAG) = (province, city_name, '') 122 elif city_input in city_dict.values(): 123 city_name = city_input 124 (P_NAME, C_NAME, C_FLAG) = (province, city_name, '') 125 else: 126 print("Wrong Input!") 127 C_NAME = '' 128 time.sleep(2) 129 130 while C_NAME: 131 framework_show(P_NAME, C_NAME, D_NAME) 132 if C_NAME in DICT_CIT.keys(): #若所选城市在城市及区字典中有记录 133 district_show(P_NAME, C_NAME) #调用城区展示函数展示城区 134 if D_FLAG == 'b': 135 break 136 else: 137 welcome_show(P_NAME, C_NAME) #若所选城市在城区字典中无记录,调用展示函数 138 time.sleep(5) 139 C_NAME = '' #展示后将城市清空,回到选择城市界面 140 break 141 142 143 144 #传入省份和城市,展示相关地区 145 def district_show(province, city): 146 global DICT_PRO 147 global DICT_CIT 148 global P_NAME 149 global C_NAME 150 global D_NAME 151 global D_FLAG 152 153 district_dict = {} 154 district_list = DICT_CIT[city] 155 for (n, d) in enumerate(district_list, 1): 156 district_dict[n] = d 157 print('%d.%s' % (n, d) + '\t', end='') 158 if (n % 4 == 0): 159 print() 160 print('\n================================================================') 161 print('q : Exit b : Back') 162 163 district_input = input('请输入区名或编号:') 164 district_input = input_verify(district_input) 165 166 if district_input == 'q': 167 exit(0) 168 elif district_input == 'b': 169 (P_NAME, C_NAME, D_NAME, D_FLAG) = (province, '', '', 'b') 170 return 171 elif district_input in district_dict.keys(): 172 district_name = district_dict[district_input] 173 (P_NAME, C_NAME, D_NAME, D_FLAG) = (province, city, district_name,'') 174 elif district_input in district_dict.values(): 175 district_name = district_input 176 (P_NAME, C_NAME, D_NAME, D_FLAG) = (province, city, district_name,'') 177 else: 178 (P_NAME, C_NAME, D_NAME, D_FLAG) = (province, city, '', '') 179 district_name = '' 180 print("Wrong Input!") 181 time.sleep(2) 182 183 if district_name: #若选中了城区,调用展示函数 184 welcome_show(P_NAME, C_NAME, D_NAME) 185 time.sleep(5) 186 D_NAME = '' #展示后将城区清空,将回到城区选择界面 187 188 189 P_NAME='' #省份全局变量 190 C_NAME='' #城市全局变量 191 D_NAME='' #城区全局变量 192 C_FLAG='' #城市选择页面输入b选项 193 D_FLAG='' #城区选择页面输入b选项 194 195 196 while True: 197 framework_show(P_NAME, C_NAME, D_NAME) 198 province_show()