码迷,mamicode.com
首页 > 编程语言 > 详细

python code 1_username registration & login

时间:2019-03-14 11:47:03      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:password   auth   pytho   color   adl   with   ogr   ...   NPU   

This tiny program consists of 2 parts - registration and login. In the part of registration, the key point is to create a folder to store user‘s information.  And the part that cost my most time is how to name a file automatically with variety‘s name. Here is the registration code:

 

 1 # Author: Eva Han
 2 
 3 for i in range(1000):
 4     r_username = input(please input your username for registration:)
 5     r_passwd = input(please input your password for registration:)
 6     r_passwd2 = input(please input your password again:)
 7 
 8     if r_passwd == r_passwd2:
 9         file_name = F:/python/计算机学习/practice/homework1/registration/+ r_username +.txt
10         regis_info = open(file_name,w+)
11         regis_info2 = ‘‘‘{_r_username}
12 {_r_passwd}‘‘‘.format(_r_username = r_username, _r_passwd = r_passwd )
13         regis_info.write(regis_info2 )
14         regis_info.flush()
15         regis_info.close()
16         break
17     else:
18         print(The passwords are diffrent, please input again)

 

Besides, there is a small point needed to be paid attention to - the content of regis_info2. Don‘t add any blank space!!! It‘s pretty important! and we need to check it again and again. Cuz in the next part, because of the blank space that I didn‘t notice, I was reminded that I input a wrong password many times even if I input the right one. = ^ =

 

In the login program, the best parts are the methods of globaling a variety, reading the content in a specific line and using count number to operate under a specific condition. The login program is shown below:

 

 1 # Author: Eva Han
 2 
 3 username = input(Please input your username for login:)
 4 user_file_name = F:/python/计算机学习/practice/homework1/registration/+ username +.txt
 5 user_info = open(user_file_name, r)
 6 
 7 # 读取第二行的信息,并存为_password变量
 8 # lines = user_info.readlines()
 9 # line2 = user_info.readline()
10 
11 
12 def func():
13     count = 0
14     global l_passwd
15     for lines in user_info:
16         count = count + 1  # 此处的起始值即为1
17         if count == 2:
18             l_passwd = lines
19             break
20 func()
21 
22 # 让用户输入密码进行判断
23 count2 = 0
24 for j in range(3):
25     password = input(please input your password for login:)
26     if l_passwd == password:
27         print(Welcome to my world!)
28         break
29     else:
30         print(wrong password!)
31         count2 += 1
32 
33 if count2 == 3:
34      print(Your account is locked...)
35      l_file_name = F:/python/计算机学习/practice/homework1/login/+username+.txt
36      login_info = open(l_file_name,w+)
37      login_info.write(username)
38      login_info.close()

 

------------------------------------------------------------------------------------------------------END----------------------------------------------------------------------------------------------------------------------------------------------------------------

python code 1_username registration & login

标签:password   auth   pytho   color   adl   with   ogr   ...   NPU   

原文地址:https://www.cnblogs.com/Eva-Han0615/p/10529208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!