Simple loop and select statements

Keywords: Python Database

I. judgment of login password

The implementation of this function mainly uses the while loop and if else judgment. It can exit the program if the input is not correct at most three times, and the file processing command is needed to realize the locking function.

①. Procedure flow chart

② program code diagram

③ program code:

 1 #Write the login interface:
 2 #insert username and password.
 3 #A welcome message is displayed after the authentication is successful.
 4 #Lock the interface after three wrong entries.
 5 username="root"
 6 password='123'
 7 count=0
 8 while count<3:
 9     user=input("Please input username:")
10     passw=input("\nPlease input password:")
11     if user==username and passw==password:
12         print("Welcome!\n")
13         break
14     else:
15         print("Access denied,please try again.\n")
16         count+=1
17 else:
18     print("You have tried three times,Interface locked.\n")

④ personal summary:

The implementation of the login entry is to match the entered user name password with the known correct user name password. If it is the same, the login will be successful. If it fails, the wrong login will be recorded in the blacklist. After three times, the entry will be locked. Here is the file operation. We can no longer call the password file or change the file permission of the blacklist after three times The file can't be written, and the data can be locked. I didn't think of how to call out the password to match when I wrote, so I simply wrote a cycle of three times of error jumping out of the program, without realizing the locking function.

II. [three level menu design]

This function implementation also uses circular statements and selection statements. If the function is improved or the code is optimized, the dictionary or logic is used for modification. The list I use stores data first, but the logic is not clear, and the general function is implemented.

①. Flow chart

② program code diagram

 

③ program code:

 1 #Create a multilevel menu
 2 #There are three levels in the menu.
 3 #You can enter each submenu in turn.
 4 
 5 shandong_list=['1.Ji'nan','2.Tai'an','3.Qingdao']
 6 jinan_list=['Changqing District','High-tech Zone','Lixia District']
 7 taian_list=['4.Ningyang County','Dongping County']
 8 qingdao_list=['Huangdao District','Laoshan District']
 9 ningyang_list=['Dong Shu town','Xiangyin Township','Magnetic kiln town']
10 b='back'
11 while b=='back':
12     print(shandong_list)
13     print("\nIf there are no number in front of the city,it means that's the last level of he meunu.")
14     a = input("Please chose one city number:")
15     if a=='1':
16         print(jinan_list)
17     elif a=='3':
18         print(qingdao_list)
19     elif a=='2':
20         while a=='2' or b=='2':
21             b='1'
22             print(taian_list)
23             print("\nIf there are no number in front of the city,it means that's the last level of he meunu.")
24             a =input("\nPlease chose one city number:")
25             if a=='4':
26                 print(ningyang_list)
27                 print("\nThis is the last level,if you want to back please input 2.")
28                 b = input("\nplease input your choose:")
29                 if b=='2':
30                     continue
31                 else:
32                     break
33             else:
34                 print(taian_list)
35                 break
36     else:
37         break
38     print("\nPlease input 'back' return the upside menu or input any other key to quit.")
39     b=input("\nplease input your choose:")
40     if b=='back':
41         continue
42     else:
43         break

④ personal summary

When I implemented the three-level menu, I also modified the logic many times. Because I didn't clear my mind in advance, there was no problem in the code, but there were frequent problems in the logic. Therefore, before writing a program, I must design the process, and also the storage of data. Because I haven't learned to call the database or the dictionary, I'll try using the list first , although the function is barely realized, it is certainly not as rigorous as the dictionary. Although there are reminders before each input, once the input is wrong, the program will be launched, which is also a loophole in the logic implementation. Continue to work hard.

 

The above two programs use simple while and if to realize some functions. If you can use function call or other syntax, the program will be more perfect.

Posted by greg on Sun, 03 Nov 2019 03:55:26 -0800