问题
I have the following code and in the login feature, the output is erroneous (a logic error). It basically prints "invalid username and password" until it gets to the right one, and then prints "correct login".
ERRONEOUS OUTPUT:
For example, with the test data: user3 and pass3, the output is:
*****LOGIN SCREEN******
Username: user3
Password: pass3
invalid username or password
invalid username or password
correct login
>>>
Here is the code in question, with reference to the LOGIN function:
usernames=["user1","user2","user3"]
passwords=["pass1","pass2","pass3"]
def main():
mainmenu()
def mainmenu():
print("****MAIN MENU****")
print("=======Press L to login :")
print("=======Press R to register :")
choice1=input()
if choice1=="L" or choice1=="l":
login()
elif choice1=="R" or choice1=="r":
register()
else:
print("please make a valid selection")
usernames=["user1","user2","user3"]
passwords=["pass1","pass2","pass3"]
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element
if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list
print("correct login")
else:
print("invalid username or password")
def register():
print("*****REGISTRATION****")
username=input("Enter a username:")
password=input("Enter a password:")
users_pass[username] = password
answer=input("Do you want to make another registration?")
if answer=="y":
register()
else:
registration_details()
def registration_details():
print(usernames)
print(passwords)
main()
I am looking for a) a solution and fix to the problem so that it only prints "correct login" once on finding the correct pair of usernames and passwords instead of looping through and printing each one
b) an explanation as to why the indentation or whatever it is that is wrong here doesn't work - as the logic (in a language like VB.Net) is sound.
I'd prefer it if the problem could be fixed without additional code (and a very simple fix) but perhaps the most elegant solution is a flag. That is, if the problem isn't just with indentation or something like that I've missed.
回答1:
In your login() function, you print for every element of the list, so you could print after the loop doing:
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
correct_login = False
for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element
if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list
correct_login = True
break
if(correct_login):
print("correct login")
else:
print("invalid user name or password")
回答2:
You can leverage exceptions to write login this way.
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
try:
index = usernames.index(username)
if password == passwords[index]:
print("correct login")
else:
print("invalid username or password")
except:
print("invalid username or password")
回答3:
You're seeing this happen because of the else statement in your login function. Basically what your code is currently doing in that function is looping through, checking to see if the username and password are equal to the current value (i.e. compare user1 == user2) if they do not equal then you automatically print the invalid user name.
Instead you should wait until you compare all of the values to print the invalid username or password message. Also - instead of continuing to loop through your values, you could add a break to stop the for loop once the valid value is found. Your login function would look something like this:
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
found = False
for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element
if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list
print("correct login")
found = True
break
if found == False:
print("invalid username or password")
That will give you only 1 instance of either correct login or invalid username or password.
来源:https://stackoverflow.com/questions/44854248/specific-indentation-error-in-nested-if-statement-in-for-loop