Python exercises-1. Simple logon interface and python exercises-1 Interface

Source: Internet
Author: User

Python exercises-1. Simple logon interface and python exercises-1 Interface

Function: Used to implement simple user logon interfaces.

Description: User information is stored in the dictionary. You need to manually write the user information to the code. You can log on for a maximum of nine times. The first attempt will first create a lock file in the program directory. If you enter quit in the user column, exit the program.
Enter the user. First, judge whether the user is locked based on the user stored in the lock file. If the user is locked, enter the user again. If the password is not locked, enter the password again. If the password is correct, the message is printed and the password is exited. If the password is incorrect, the number of attempts to the user increases by 1.
After the user's password is wrong three times, the lock file will be written and the program will be exited.

The source code is as follows:

#/Usr/bin/env python
#-*-Coding: UTF-8 -*-

Import OS

User_dic = {
'Jyd ': {'Password': 'abc', 'Count': 0 },
'Test': {'Password': 'test', 'Count': 0}
}
If not OS. path. isfile ('lock '):
Open ('lock', 'w'). close ()
Total_error = 0
Print ("Exit from the username and enter: quit ")
While total_error <9:
User = input ('username --> :')
If user = 'quit ':
Break
If user in user_dic.keys ():
# Check whether the user exists in the lock file. If yes, the user is locked and the value of lock_u is set to yes. Then, the lock judgment loop is displayed. Determine whether the user is locked based on the value of lock_u.
Lock = open ('lock', 'R ')
For lock_u in lock. read (). split ('\ n '):
If user = lock_u:
Print ('user % s is in a locked state' % User)
Lock_u = 'yes'
Break
Lock. close ()
If lock_u = 'yes': continue
Passwd = input ('password --> :')
If passwd = user_dic [user] ['Password']:
Print ('login successfully! ')
Break
Else:
Print ('password error ')
If user_dic [user] ['Count'] <3:
User_dic [user] ['Count'] + = 1
Else:
Lock = open ('lock', 'A ')
Lock. write (user + '\ n ')
Print ('user % s has been locked' % User)
Break
Else:
Print ('username does not exist .')
Total_error + = 1

Related Article

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.