python作业
-
Day1作业
- 作业一:编写登录接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输入三次错误后锁定用户
- 作业一:编写登录接口
1 import sys 2 3 locking = ('login/locking.txt') 4 login = ('login/login.txt') 5 6 def main(): 7 count_min = 0 8 count_max = 3 9 10 while count_min < count_max: 11 username = input("请输入用户名:") # 引导用户输入用户名 12 with open(locking, 'r') as locking_file: # 打开记载锁定用户的文件 13 for line in locking_file.readlines(): 14 if username == line.strip(' '): # 判断用户是否在锁定的用户文件里 15 sys.exit('%s用户已经被锁定。。。' % username) 16 17 password = input("请输入用户密码:") # 引导用户输入密码 18 with open(login, 'r') as login_file: 19 for line in login_file.readlines(): 20 user,passwd = line.strip(' ').split() # 对记载用户名和密码的文件进行格式化并且分别赋值给user和passwd 21 flog = False # 定义一个变量 22 if username == user and password == passwd: # 判断用户名密码是否正确 23 print("登陆成功!") 24 flog = True # 对赋值的变量进行修改已保证退出程序 25 break 26 if flog == False: 27 if count_min < 2: 28 print("用户名密码错误,请重新输入!") 29 else: 30 lock(username) # 输入错误三次则把用户加入锁定文件 31 count_min += 1 32 else: 33 break 34 35 def lock(username): 36 with open(locking, 'a') as locking_file: 37 locking_file.write(username) 38 locking_file.write(' ') 39 40 if __name__ == '__main__': 41 main()
-
- 作业二:多级菜单
- 三级菜单
- 可以依次选择进入各自菜单
- b:返回上层菜单
- q:退出程序
- 作业二:多级菜单
1 import sys 2 3 dic = {'河北省':{'辛集市':['平山县','井陉县','栾城县','正定县',],'晋州市':['迁西县','滦南县','玉田县','唐海县','乐亭县',],'邯郸市':['邯郸县','永年县','曲周县','馆陶县',]},'山西省':{'太原市':['平顺县','襄垣县','沁源县','屯留县',],'古交市':['泽州县','陵川县','阳城县','沁水县',]}} 4 5 def help(command): # 打印帮助文档 6 print (''' 7 i 进入菜单 8 b 返回上层菜单 9 q 退出程序 10 xxxxx 输入省市县全称进入下一级 11 ''') 12 13 def province(command): # 打印第一级菜单 14 province = dic.keys() 15 i = 1 16 for province in province: 17 print(str(i) + '.' + province) 18 i += 1 19 20 def city(command): # 打印第二级菜单 21 city = dic.get(command) 22 i = 1 23 for city in city.keys(): 24 print(str(i) + '.' + city) 25 i += 1 26 27 def county(command): # 打印第三级菜单 28 county = city_dic.get(command) 29 # print(county) 30 i = 1 31 for county in county: 32 print(str(i) + '.' + county) 33 i += 1 34 35 def q(command): 36 sys.exit('欢迎下次查询') 37 38 def main(): 39 print (''' 40 帮助命令help 41 ''') 42 while True: 43 command = input("province > ") # 引导用户输入 44 if command in 'help' or command == '?': 45 help(command) 46 elif command == 'i': 47 province(command) 48 while True: 49 li = list(dic.keys()) 50 command = input("city > ") 51 # print(command) 52 # print(type(command)) 53 if command == 'b': 54 # print(1) 55 break 56 elif command == 'q': 57 q(command) 58 elif command in li: 59 city(command) 60 city_dic = dic.get(command) 61 city_li = list(city_dic.keys()) 62 while True: 63 command = input("county > ") 64 # print(city_li) 65 # print(command) 66 global city_dic 67 if command in city_li: 68 county(command) 69 # print(command) 70 elif command == 'b': 71 break 72 elif command == 'q': 73 q(command) 74 elif len(command) == 0: 75 continue 76 else: 77 print('输入错误,请重新输入') 78 continue 79 elif len(command) == 0: 80 continue 81 else: 82 print('输入错误,请重新输入') 83 continue 84 elif command == 'q': 85 q(command) 86 elif len(command) == 0: 87 continue 88 else: 89 print('输入错误,请重新输入。。。') 90 91 if __name__ == '__main__': 92 main()