def login():
logindetails = {'username':'Admin123' , 'password':'Pass123'}
username = input("please enter the username")
password = input("please enter the password")
if username == 'Admin123':
print ("username correct")
else:
print ("username incorrect")
if password == 'Pass123':
print ("password correct")
else:
print ("password incorrect")
login()
Please tell me where I would put the while loop and how would I format the code to add a counter too.
First, you should initialize username and password variables to empty strings as well as your counter.
Then, you should declare your while loop with for condition, the comparison between
If both are the same, you're getting outside the loop, if not, you have to ask again for user inputs and increment the counter.
In other words :
username = ""
password = ""
counter = 0
WHILE username != logindetails['username'] AND password != logindetails['password']:
ASK for user inputs / DISPLAY error messages
INCREMENT counter
DISPLAY "logged after counter attempts"
Does it answer your questions ?
You can do something like this:
def login():
logindetails = {'username':'Admin123' , 'password':'Pass123'}
username = input("please enter the username")
password = input("please enter the password")
valid = False
while valid == False:
if username == 'Admin123' and password == 'Pass123':
print ("username and password correct")
valid = True
pass
else:
print ("username or password incorrect")
username = input("please enter the username")
password = input("please enter the password")
valid is a bool so the loop wont stop until the username and password is correct.
Or you can have a fail safe where if the user enters the username and password wrong more than say 4 times, it kicks them out.
def login():
logindetails = {'username':'Admin123' , 'password':'Pass123'}
username = input("please enter the username")
password = input("please enter the password")
valid = False
count = 0
while count != 4 or valid == False:
if username == 'Admin123' and password == 'Pass123':
print ("username and password correct")
valid == True
pass
else:
print ("username or password incorrect")
count += 1
username = input("please enter the username")
password = input("please enter the password")
if valid == False:
print ("Sorry you had too many attempts to login. Try again later")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.