After landing the interface written by Python, a user name and password authentication is successful display login information, user name and password wrong three times no further input

Method a: for loop implemented
first define two variables _user, _passwd
, save the user name and password, then the number of inputs for the control loop, with a username variable, the value stored Password entered,
are used to verify the user name and password verification is successful successful landing information is displayed, otherwise an error message. code show as below:
= _user "FZW" 
_passwd = "123"
for I in Range (. 3):
username = INPUT ( "the Username:")
Password = INPUT ( "Password:")
IF username and Password == == _user _passwd:
Print ( " Welcome to S% "% _user)
BREAK # out of the loop
else:
print (" user name or password is incorrect ")!
else: # long for the normal cycle is finished, it will execute the else statement
print (" has been entered three times ")!

run The results shown in FIG successful landing failure of the landing method two: implemented while loop while loop is to record the number of cycles by the variable count, the code follows: _user = "FZW"







_passwd = "123"
count = 0
while count < 3: #当while后面的条件成立,才执行下面的语句
username = input("Username:")
Password = input("Password:")
if username == _user and Password == _passwd :
print("欢迎%s" %_user)
break #跳出循环
else:
print("用户名或密码错误!")
count +=1
else:
print("已经输入过三次!")


运行结果同上图


Guess you like

Origin www.cnblogs.com/fzw1995/p/11305548.html