Path to python-basics-Job 1 Write logon interface, python Basics
Job Requirements:
① Enter the user name and password
② Display the welcome information after successful authentication
③ Lock after three wrong attempts
Flowchart:
The picture is not clear, please move your feet to the http://pan.baidu.com/s/1pKqlXQr
#! /Usr/bin/env python #-*-coding: UTF-8-*-# file_name: user_login.pylock = 0lock_value = 'no' lock _ dict = {} user_dict = {} f1 = open('lock.txt ', 'r +') f2 = open('user.txt ', 'R ') for I in f1.readlines (): user, number = I. strip (). split () lock_dict [user] = numberfor I in f2.readlines (): user, passwd = I. strip (). split () user_dict [user] = passwdwhile lock <3: # enter the cyclic username = input ("Enter the user name:") pass Wd = input ("Enter Password:") if lock_dict.get (username): # username exists in lock.txt if int (lock_dict.get (username)> = 3: print ('account locked. Please enter it again! ') Lock + = 1 continue if user_dict.get (username): # if passwd = user_dict.get (username) exists in user_dict: # print if the password is correct ('login successful ') break else: # incorrect password if lock_dict.get (username): # username in lock_dict [username] = int (lock_dict [username]) + 1 lock_value = 'yes' else: # username does not exist in lock_dict [username] = 1 lock_value = 'yes' else: # username does not exist in user_dict lock_value = 'Yes 'If lock_value = 'yes': print ('account Password error, please enter 'Again) lock + = 1 continueelse: # print more than 3 times ('Sorry, the number of inputs has exceeded the upper limit! ') # Clear lock.txt f1.seek (0) f1.truncate (0) for I in lock_dict: username = I number = lock_dict [I] f1.write ('% s \ t % s \ n' % (username, number) f1.close () f2.close ()
Auxiliary file:
① User.txt zhangcong 123 admin 123 zhangsan 123 ② lock.txt is empty