I was trying to create a login system that will check if the username and password is in the list or not. The problem that I have with the code below is that when the first array of credentials does not match, it will checks the first user-password pair only for the next input. May I know what's the reason for this error and how can I avoid it?
userlist = [["k",'l'],['m','z'],['l','o']] #dummy data
def signin():
username = input("Please enter username: ")
for user in userlist:
if username == user[0]:
password = input("Please enter password: ")
if password == user[1]:
print("correct password")
signin()
else:
print("Incorrect password")
signin()
else:
print("Unregister username")
signin()
signin()
Output:
Please enter username: k
Please enter password: l
correct password
Please enter username: m
Unregister username
Please enter username: l
Unregister username
Please enter username:
Expecting output:
Please enter username: k
Please enter password: l
correct password
Please enter username: m
Please enter password: z
correct password
Please enter username: l
Please enter password: o
correct password
Please enter username:
Back space your last else so that it is inline with your for loop. Why? Well, right now if you enter l at the first input, it will check this list ["k",'l']
. It is not there so it will go to the else. Putting the else outside the for loop makes sure we iterate through the entire list before making a decision.
userlist = [["k",'l'],['m','z'],['l','o']] #dummy data
def signin():
username = input("Please enter username: ")
for user in userlist:
if username == user[0]:
password = input("Please enter password: ")
if password == user[1]:
print("correct password")
signin()
else:
print("Incorrect password")
signin()
else:
print("Unregister username")
signin()
signing()
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.