#!/usr/bin/env pthon #字典操作三级菜单 “b”返回上一级菜单,“q”退出。 menu={"BJ":{"cp":{1:1,2:2,3:3}, "ft":{1:4,2:5,3:6}}, "SH":{"lz":{1:1,2:2,3:3} }, "HK":{"tz":{1:1,2:2,3:3}, "fs":{1:4,2:5,3:6}}} def one_menu(): for i in range(0,len(menu.keys())): print(str(i+1) +"."+ menu.keys()[i]) i+=1 one_menu() def tow_menu(city): menu_list=menu.get(city).keys() for i in range(0,len(menu_list)): print (str(i+1) + "." + menu_list[i]) i+=1 def three_menu(city,area): area_menu=menu.get(city).get(area) for i in range(0,len(area_menu)): print(area_menu.items()[i]) chose_num="b" while chose_num !="q": chose_num=raw_input("please chose menu:") if chose_num =="1": tow_menu("HK") while chose_num !="q": chose_num=raw_input("please chose menu:") if chose_num=="1": three_menu("HK","fs") elif chose_num=="2": three_menu("HK","tz") elif chose_num=="b": tow_menu("HK") break elif chose_num=="q": exit() else: print("input error!") elif chose_num =="2": tow_menu("SH") while chose_num !="q": chose_num=raw_input("please chose menu:") if chose_num=="1": three_menu("SH","lz") elif chose_num=="b": tow_menu("SH") break elif chose_num=="q": exit() else: print("input error!") elif chose_num =="3": tow_menu("BJ") while chose_num !="q": chose_num=raw_input("please chose menu:") if chose_num=="1": three_menu("BJ","ft") elif chose_num=="2": three_menu("BJ","cp") elif chose_num=="b": tow_menu("BJ") break elif chose_num=="q": exit() else: print("input error!") elif chose_num =="b": one_menu() elif chose_num =="q": exit() else: print("input error!")
#文件操作用户登录,提示用户名不存在 和 密码错误,密码错误超过3次则锁定用户登录。
1 #!/usr/bin/env python 2 # -*-coding:UTF-8-*- 3
4 5 6 def login(): 7 8 f=open("user",'r') #读取user配置文件。 9 cont=f.readlines() #readlines把读取的行当作元素返回一个列表 10 f.close() 11 allname=[] #初始化一个用户列表 12 allpasswd=[] 13 for i in range(0,len(cont)-1): #len获取cont列表的元素数量 14 onecont=cont[i].split() #循环取一行内容并分割成列表,split()以空格为分隔符分割字符串并返回一个列表。 15 onename=onecont[0] #循环取一行中的帐号 16 onepasswd=onecont[1] # 17 allname.append(onename) #循环把每一行取到的帐号追加到用户列表中 18 allpasswd.append(onepasswd) 19 lf=open("user.lock",'r') 20 lcont=lf.readlines() 21 lf.close() 22 # print(lcont) #打印用户锁文件内容 23 # print(allname) 24 # print(allpasswd) 25 26 cont=0 27 while cont < 3: 28 username=raw_input("login user:").strip() 29 passwd=raw_input("password:") 30 if username not in allname: 31 print("No this accont!") 32 33 elif (username +"\n") in lcont: 34 print("your account has been locked!Please contact admin!") 35 break 36 else: 37 rel_passwd_index=allname.index(username) #取该帐号在用户列表中的索引,此时用户列表的索引和密码列表的索引是对应的,因此我们同样>取到了该帐号的密码在密码列表的索引 38 rel_passwd=allpasswd[rel_passwd_index] #取该帐号的真实密码 39 if passwd==rel_passwd: 40 print("Login success!") 41 break 42 else: 43 print("password is error!") 44 cont+=1 45 if cont >= 3: 46 print("Excessive password error,your account has been locked!Please contact admin!") 47 nf=open("user.lock",'wb') 48 nf.write(username+"\n") 49 nf.close() 50 51 52 login()
51_陆雅亮