作业二:编写登陆接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
作业三:多级菜单
- 三级菜单
- 可依次选择进入各子菜单
- 所需新知识点:列表、字典
原博客链接:http://www.cnblogs.com/alex3714/articles/5465198.html
作业二:
1 # Author:Tang Ziyue 2 3 #打开存储用户名密码文件和存储被锁定用户名的文件 4 f = open('E:/usr.txt','r') 5 f2 = open('E:/lock_usr.txt','r') 6 7 dict = {} 8 list = [] 9 10 #将文件中的数据转为字典 11 for line in f.readlines(): 12 line = line.strip() #删除空白符 13 if not len(line): 14 continue 15 dict[line.split(':')[0]] = line.split(':')[1] 16 f.close() 17 18 for line2 in f2.readlines(): 19 line2 = line2.strip() 20 if not len(line2): 21 continue 22 list.append(line2) 23 f2.close() 24 25 print(dict) 26 print(list) 27 28 count = 0 29 username = input("用户名:") 30 if username in list: 31 print("此用户已被锁定!") 32 exit(0) 33 if username in dict: 34 while count<3: 35 password = input("密码:") 36 if password == dict.get(username): 37 print("欢迎",username) 38 break 39 else: 40 print("密码错误,请再次输入!") 41 count += 1 42 if count == 3: 43 print("密码尝试多次,已锁定!") 44 f2 = open('E:/lock_usr.txt','w') 45 f2.write(username) 46 f2.close() 47 else: 48 print("此用户名不存在!")
作业三:
1 # Author:Tang Ziyue 2 3 country = ['中国','美国'] 4 China = { 5 '天津':{'南开区','河东区','河西区','和平区','东丽区'}, 6 '北京':{'东城区','西城区','朝阳区','海淀区','通州区','大兴区'} 7 } 8 9 10 print("如需返回上一级请输入b") 11 print(country) 12 while True: 13 coun = input("请输入你要选择的国家:") 14 if coun == "中国": 15 print(China.keys()) 16 while True: 17 city = input("请输入你要选择的城市或返回上一级:") 18 if 'b' == city: 19 break 20 elif city in China.keys(): 21 print(China.get(city)) 22 if 'b' == input(): 23 continue 24 else: 25 print("对不起,我现在还不知道美国的城市!")
注:Pycharm, Python3
浙公网安备 33010602011771号