day1作业脚本

1.编写登录接口:

- 输入用户名和密码

- 认证成功后显示欢迎信息

- 输错三次后锁定

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import sys,os
 5 
 6 dir = os.getcwd()
 7 
 8 Count = 0
 9 
10 STATUS = True
11 while STATUS:
12     L_user = input("you username:").strip()
13     with open(dir+"lock.txt","r+") as f:
14         for n in f.readlines():
15             if L_user == n.strip("
"):
16                 print("username is lock!!!")
17                 sys.exit()
18     L_passwd = input("you passwd:")
19     with open(dir+"passwds.txt","r+") as f1:
20         for line in f1.readlines():
21             user,passd = line.strip("
").split(":")
22             if L_user == user and L_passwd ==passd:
23                 print("Welcome to login successfully")
24                 sys.exit()
25         else:
26             print("login faile")
27     Count += 1
28     if Count == 3:
29         print("sorry ,then three time ,username lock!!!")
30         with open(dir+"lock.txt","a+") as f2:
31             f2.write(L_user+"
")
32             STATUS = False

2.编写多级菜单

- 三级菜单

- 可依次进入子菜单

第一次写python脚本,因为没有学到函数,所以写的有点糙和繁琐,我用自己学到的知识来实现这些需求。大家也可试一试。

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys

meau = {
    "北京":{
        "通州":["梨园","果园","土桥"],
        "朝阳":["将台","望京","关庄"],
        "海淀":["色色","吸收","里是"]
    },
    "天津":{
        "滨海":["但是","分手","而是"],
        "光华":["厚爱","感觉","色鬼"],
        "经济":["回来","反对","沟通"]
    },
    "河北":{
        "邯郸":["是啊","在啊","怕看"],
        "邢台":["个是","的个","是和"],
        "保定":["先动","个他","不发"]
    },
}

Shi = []
Status = True
try:
    while Status:
        for k in meau:
            print(k)
        choose = input("b:back,q:quie,choose:")
        if choose in meau:
            Shi.append(meau)
            meau = meau[choose]
        elif choose == "b":
            if Shi:
                meau = Shi.pop()
        elif choose == "q":
            Status = False
     else:
      print("input error")
except TypeError as e: print(choose)

  

  1 #!/usr/bin/env python
  2 #NameFile:test.py
  3 #Import time module
  4 import time
  5 import sys
  6 #Define a number of variables
  7 sum = 0
  8 H = {'1':'白石桥','2':'东直门','3':'复兴路'}
  9 K = {'1':'灵石路','2':'宝山路','3':'康乐路'}
 10 P = {'1':'和平路','2':'辽宁路','3':'新华路'}
 11 J = {'1':'大井湾','2':'王家湾','3':'茅溪村'}
 12 B = {'1':'海淀区','2':'朝阳区','3':'东城区','4':'西城区'}
 13 S = {'1':'黄浦区','2':'虹口区','3':'长宁区','4':'徐汇区'}
 14 T = {'1':'和平区','2':'河东区','3':'河北区','4':'河西区'}
 15 C = {'1':'江北区','2':'长寿区','3':'荣昌区','4':'巴南区'}
 16 City = {
 17     '1':'北京市',
 18     '2':'上海市',
 19     '3':'天津市',
 20     '4':'重庆市',
 21 }
 22 #Registered account
 23 user = input("User:").strip()
 24 if len(user) == 0 :
 25     print ("User cannot be empty!!!")
 26 elif len(user) > 6:
 27     print ("Your username is too long!!")
 28 passwd = input("Passwd:")
 29 if len(passwd) == 0 :
 30     print ("Passwd cannot be empty!!!")
 31 elif len(passwd) > 6:
 32     print ("You set the password is too long!!")
 33 #Format the user's account and password
 34 list = """
 35      Your account password is as follows:
 36      user:%s
 37      passwd:%s"""%(user,passwd)
 38 print (list)
 39 print ("Congratulations on your registration,Please login again after five seconds.")
 40 #Wait five seconds to allow the user to re-enter
 41 time.sleep(5)
 42 #Please login again
 43 while sum < 3:
 44     User = input("please you input user:").strip()
 45     if len(User) == 0 :
 46         print ("User cannot be empty!!!")
 47     elif len(User) > 6:
 48         print ("Your username is too long!!")
 49     Passwd = input("please you input passwd:")
 50     if len(Passwd) == 0 :
 51         print ("paswd cannot be empty!!!")
 52     elif len(Passwd) > 6:
 53         print ("You set the password is too long!!")
 54     if User == user and Passwd == passwd:
 55         print ("Welcome you to log in.After five seconds to enter the selection interface")
 56         break
 57     else:
 58         print ("Your account or password is incorrect. Please log in again.!!!")
 59 #Number of users log on to count more than three times to lock the account
 60         sum += 1
 61         if sum > 4:
 62             print ("You log in too many, will lock your user!!!!")
 63 time.sleep(5)
 64 for k,v in City.items():
 65     print (k,v)
 66 while True:
 67     print ('Q:退出')
 68     A = input('Please select:').strip()
 69     if A == '1':
 70         for k,v in B.items():
 71           print (k,v)
 72         print ('Q:退出')
 73         G = input ('Please continue to choose:').strip()
 74         if G == '1':
 75             for k,v in H.items():
 76                 print (k,v)
 77             print ('Q:退出')
 78             Y = input ('please continue:').strip()
 79             if Y == '1':
 80                 print ('You choose is:',H['1'])
 81                 sys.exit()
 82             elif Y == '2':
 83                 print ('You choose is:', H['2'])
 84                 sys.exit()
 85             elif Y == '3':
 86                 print ('You choose is:',H['3'])
 87                 sys.exit()
 88             elif Y == 'Q':
 89                 print ('Welcome to come again next time.')
 90                 sys.exit()
 91         if G == 'Q':
 92             print ('Welcome to come again next time.')
 93             sys.exit()
 94     if A == '2':
 95        for k,v in S.items():
 96          print (k,v)
 97          print ('Q:退出')
 98        G = input ('Please continue to choose:').strip()
 99        if G == '1':
100             for k,v in K.items():
101                 print (k,v)
102             print ('Q:退出')
103             Y = input ('please continue:').strip()
104             if Y == '1':
105                 print ('You choose is:',K['1'])
106                 sys.exit()
107             elif Y == '2':
108                 print ('You choose is:', K['2'])
109                 sys.exit()
110             elif Y == '3':
111                 print ('You choose is:',K['3'])
112                 sys.exit()
113             elif Y == 'Q':
114                 print ('Welcome to come again next time.')
115                 sys.exit()
116        if G == 'Q':
117             print ('Welcome to come again next time.')
118             sys.exit()
119     if A == '3':
120        for k,v in T.items():
121          print (k,v)
122        print ('Q:退出')
123        G = input ('Please continue to choose:').strip()
124        if G == '1':
125             for k,v in P.items():
126                 print (k,v)
127             print ('Q:退出')
128             Y = input ('please continue:').strip()
129             if Y == '1':
130                 print ('You choose is:',P['1'])
131                 sys.exit()
132             elif Y == '2':
133                 print ('You choose is:', P['2'])
134                 sys.exit()
135             elif Y == '3':
136                 print ('You choose is:',P['3'])
137                 sys.exit()
138             elif Y == 'Q':
139                 print ('Welcome to come again next time.')
140                 sys.exit()
141        if G == 'Q':
142             print ('Welcome to come again next time.')
143             sys.exit()
144     if A == '4':
145        for k,v in C.items():
146          print (k,v)
147          print ('Q:退出')
148        G = input ('Please continue to choose:').strip()
149        if G == '1':
150             for k,v in J.items():
151                 print (k,v)
152             print ('Q:退出')
153             Y = input ('please continue:').strip()
154             if Y == '1':
155                 print ('You choose is:',J['1'])
156                 sys.exit()
157             elif Y == '2':
158                 print ('You choose is:', J['2'])
159                 sys.exit()
160             elif Y == '3':
161                 print ('You choose is:',J['3'])
162                 sys.exit()
163             elif Y == 'Q':
164                 print ('Welcome to come again next time.')
165                 sys.exit()
166        if G == 'Q':
167             print ('Welcome to come again next time.')
168             sys.exit()
169     if A == 'Q':
170         print ('Welcome to come again next time.')
171         sys.exit()
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys

meau = {
"北京":{
"通州":["梨园","果园","土桥"],
"朝阳":["将台","望京","关庄"],
"海淀":["色色","吸收","里是"]
},
"天津":{
"滨海":["但是","分手","而是"],
"光华":["厚爱","感觉","色鬼"],
"经济":["回来","反对","沟通"]
},
"河北":{
"邯郸":["是啊","在啊","怕看"],
"邢台":["个是","的个","是和"],
"保定":["先动","个他","不发"]
},
}

Shi = []
Status = True
try:
while Status:
for k in meau:
print(k)
choose = input("b:back,q:quie,choose:")
if choose in meau:
Shi.append(meau)
meau = meau[choose]
elif choose == "b":
if Shi:
meau = Shi.pop()
elif choose == "q":
Status = False
except TypeError as e:
print(choose)
原文地址:https://www.cnblogs.com/nansenblog/p/5111736.html