Python3 Simple login, register test code

Source: Internet
Author: User

‘‘‘

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.