‘‘‘
Write a login, register the simple code. Simple test, simple function, no too many conditions to judge. Just for practice.
‘‘‘
def login (Username,password):
‘‘‘
DB the path to this file is the current directory
Open the file to save the account password, read the password in the file and save it in a dictionary format.
If the entered account already exists in the dictionary key, the result returns false.
Returns true if the account has a dictionary and the corresponding key value equals the password entered.
‘‘‘
Fo=open (' db ', ' R ')
dict1={}
For i in FO:
I=i.strip (' \ n ')
Li=i.split (")
Dict1.setdefault (Li[0],li[1])
If username in Dict1.keys () and Dict1[username]==password:
Return True
Else
Return False
Fo.close ()
def register ():
‘‘‘
Open the file where you saved your account password and save the account name as a separate list.
If the account you entered already exists in this list, the account already exists
If the account entered does not exist in this list, then write the account and password to the open file to save.
‘‘‘
New_user=input (' Please enter the user name to be requested: ')
Fr=open (' db ', ' R ')
Info_lists=fr.readlines ()
Name_lists=[]
For I in Info_lists:
Name=i.strip (' \ n '). Split (') [0]
Name_lists.append (name)
If New_user not in name_lists:
New_pwd=input (' Please enter password: ')
Fo=open (' db ', ' a ')
Up= ' \ n ' +new_user+ ' +new_pwd
Fo.write (UP)
Fo.close ()
Else
Print (' User name already exists ')
Fr.close ()
# #
def main ():
‘‘‘
Program entry, select login or register
Call the login function or register the function to indicate the success or failure of the registration based on the return value.
‘‘‘
Info=input (' 1,login;2,register: ')
If info== ' 1 ':
User=input (' User name: ')
Pwd=input (' Password: ')
If login (user,pwd):
Print (' Chenggong ')
Else
Print (' Shibai ')
Elif info== ' 2 ':
Register ()
Main ()
This article is from the "Merry_christmas" blog, make sure to keep this source http://eiouwaikiu.blog.51cto.com/7890868/1872042
Python3 Simple login, register test code