python成长之路-----day1-----作业(登录程序和三级菜单)

作业:

作业1:用户登录

1)程序说明:

a.用户输入密码验证成功然后打印欢迎信息

b.如果密码错误,用户登录失败,提示用户,密码错误

c.用户输入密码错误3次,则用户锁定

d.当用户多次输入不存在的用户时,提示错误信息,并让用户等待10s之后再次输入

账户信息保存在user.txt中,例子:dean,dean123,0 这三个值分别表示的含义:第一位是:username,第二位是:password,第三位表示用户的状态,0表示正常,1表示锁定

2)流程图如下:

3)程序代码:

 1 #AUTHOR:FAN
 2 import time
 3 #定义一个用户字典
 4 user_dict={}
 5 #定义一个用户列表
 6 user_list=[]
 7 
 8 f=open("user.txt",'r')
 9 #用for循环获取文件中每行的内容,并写入到字典中,value_interm[0]表示username,value_interm[1]表示password,value_interm[2]表示
10 #账户的锁定信息:0表示正常,1表示锁定
11 for line in f.readlines():
12     useriterm = line.strip()
13     value_interm = useriterm.split(',')
14     value_username = value_interm[0]
15     value_password = value_interm[1]
16     value_lock = value_interm[2]
17     user_dict[value_username]={
18         "name":value_username,
19         "password":value_password,
20         "lock":value_lock
21     }
22 f.close()
23 #print(user_dict)
24 #定义个count_num用户计算用户输入错误用户的次数
25 count_num = 0
26 #用于跳出多层循环
27 flag = True
28 while flag:
29     if count_num == 3:
30         print("dute to input non-existent user ,you need to wait 10s")
31         time.sleep(10)
32     # 获取用户输入的用户名
33     user_name = input("please input your username:")
34     if user_name in user_dict.keys():
35         #print(type(user_dict[user_name]["lock"]))
36         #判断用户是否被锁定
37         if int(user_dict[user_name]["lock"]) == 0:
38             for i in range(3):
39                 password = input("please input you password:")
40                 #判断密码是否正确
41                 if password == user_dict[user_name]["password"]:
42                     print("welcome to login my system!")
43                     flag=False
44                     break
45                 else:
46                     print("password is error")
47             else:
48                 #用户输入密码错误三次后被锁定
49                 user_dict[user_name]["lock"]="1"
50                 f = open("user.txt","w+")
51                 #将字典装换成列表,将改变的信息写入到文件中
52                 for value in user_dict.values():
53                     user_list =[value["name"],value["password"],value["lock"]]
54                     user_list =",".join(user_list)
55                     f.write(user_list+"\n")
56                 print("you input wrong password too many,the user is locked")
57                 break
58         else:
59             print("user is locked")
60     else:
61         print("user is not exist")
62         count_num+=1

 作业2:三级菜单

1)程序说明:

a.用户运行程序后,打印第一级菜单

b.用户输入相应的编号进入下一级菜单,当编号错误的时候会提示用户输入错误,并让用户重新输入

c.用户在每一级的时候,都能通过输入q退出程序,并能通过输入up返回到上级菜单,返回到上级目录时要将上级目录进行打印

d.用户到最后一级的时候提示用户已经到最后一级目录,可以直接退出程序或返回上级目录

2)流程图如下:

3)程序代码:

  1 #AUTHOR:FAN
  2 #定义一个菜单字典
  3 menu_dict={
  4     "河南省":{
  5         "焦作市":{
  6             "修武县":{"AA","BB","CC"},
  7             "武陟县":{"DD","EE","FF"},
  8             "博爱县":{"GG","HH","II"}
  9         },
 10         "新乡市":{
 11             "辉县":{"AA","BB","CC"},
 12             "封丘县":{"DD","EE","FF"},
 13             "延津县":{"GG","HH","II"}
 14         }
 15     },
 16     "河北省":{
 17         "邢台":{
 18             "宁晋县":{"AA","BB","CC"},
 19             "内丘县":{"DD","EE","FF"},
 20             "邢台县":{"GG","HH","II"}
 21         },
 22         "唐山":{
 23             "乐亭县":{"AA","BB","CC"},
 24             "唐海县":{"DD","EE","FF"},
 25             "玉田县":{"GG","HH","II"}
 26         }
 27     }
 28 }
 29 #用户退出多级菜单用flag
 30 flag =True
 31 while flag:
 32     #用于记录一级菜单key的个数
 33     count1 = 0
 34     #打印一级菜单
 35     for index,key in enumerate(menu_dict.keys()):
 36         count1+=1
 37         print(index,key)
 38     user_choice = input("please input your choice:")
 39     #判断用户输入的值是否为全数字
 40     if user_choice.isdigit() is True:
 41         user_choice = int(user_choice)
 42         #判断用户输入的值是否大于列表的最大值
 43         if user_choice < count1:
 44             #将字典转换成列表,menu_dict.keys()默认不是列表
 45             menu_list = list(menu_dict.keys())
 46         else:
 47             print("you need input right num")
 48             continue
 49     elif user_choice == "q":
 50         flag= False
 51         break
 52     else:
 53         print("please input a num")
 54         continue
 55     while flag:
 56         count2= 1
 57         for index,key in enumerate(menu_dict[menu_list[user_choice]].keys()):
 58             count2+=1
 59             print (index,key)
 60         user_choice2 = input("please input your choice(q:退出程序,up上一级):")
 61         if user_choice2.isdigit() is True:
 62             user_choice2 = int(user_choice2)
 63             if user_choice2 < count2:
 64                 menu_list2 = list(menu_dict[menu_list[user_choice]].keys())
 65             else:
 66                 print("you need input right num")
 67                 continue
 68         elif user_choice2 == "q":
 69             flag=False
 70             break
 71         elif user_choice2 =="up":
 72             break
 73         else:
 74             print("please input a num")
 75             continue
 76         while flag:
 77             count3=0
 78             for index,key in enumerate(menu_dict[menu_list[user_choice]][menu_list2[user_choice2]].keys()):
 79                 print(index,key)
 80                 count3+=1
 81             user_choice3 =input("please input your choice(q:退出程序,up上一级):")
 82             if user_choice3.isdigit() is True:
 83                 user_choice3=int(user_choice3)
 84                 if user_choice3 < count3:
 85                     menu_list3 = list(menu_dict[menu_list[user_choice]][menu_list2[user_choice2]])
 86                 else:
 87                     print("you need input right num")
 88                     continue
 89             elif user_choice3 == "up":
 90                 break
 91             elif user_choice3 =="q":
 92                 flag =False
 93                 break
 94             else:
 95                 print("please input a num")
 96                 continue
 97             while flag:
 98                 for index,key in enumerate(menu_dict[menu_list[user_choice]][menu_list2[user_choice2]][menu_list3[user_choice3]]):
 99                     print (index,key)
100                 #提示用户已经到最后一级目录,可以退出或返回上级目录
101                 q_or_up = input("This is last level,you want to q(退出程序) or up(上一级):")
102                 if q_or_up == "q":
103                     flag = False
104                     break
105                 elif q_or_up == "up":
106                     break
107                 else:
108                     continue

 

 

 


posted @ 2016-07-26 11:18  fan-tastic  阅读(2131)  评论(0编辑  收藏  举报