0

I'm making a simple login system in Python where you have to fill in an email adress and password. When the email and password you just filled in are true, it should only display that and not that the other possible emails and passwords are wrong, as listened below:

loginInformation = {
    1: {"firstName": "Bert", "lastName": "de Vries", "emailAdress": "bert@gmail.com", "password": "volkswagen"},
    2: {"firstName": "Kristina", "lastName": "van Goor", "emailAdress": "kris94@hotmail.com", "password": "feyenoord"},
    3: {"firstName": "John", "lastName": "Doe", "emailAdress": "johndoe@gmail.com", "password": "paashaas"},
    4: {"firstName": "Sophia", "lastName": "Smith", "emailAdress": "soso@outlook.com", "password": "kerstman"}
}

givenEmailAdress = input("Please enter your email adress: ")
givenPassword = input("Thank you! Please enter now your password: ")

for key, info in loginInformation.items():
    firstName = info["firstName"]
    lastName = info["lastName"]
    emailAdress = info["emailAdress"]
    password = info["password"]

    if(givenEmailAdress == emailAdress and givenPassword == password):
        print("Hello",firstName,lastName,", you have successfully logged in.")
    else:
        print("Your password and email adress are wrong.")

Example: When you fill in key 3, it should show:

Hello John Doe , you have successfully logged in.

But it is unfortunately showing:

Your password and email adress are wrong.
Your password and email adress are wrong.
Hello John Doe , you have successfully logged in.

What am I doing wrong? I assume it should be a quick fix. Thanks in advance!

JvW
  • 45
  • 4
  • You need at least a `break` statement after the success case, and you'll need to tweak your logic if you don't want "Your password and email address are wrong" to be repeated n times. – RJFalconer Aug 02 '22 at 21:31
  • Thanks for your comment! I've now put the if/else statement out of the for-loop, so that the "Your password and email address are wrong" won't be unnecessary repeated. However, now only the last key is shown as correct and the other thee as wrong (although I filled them in correctly). Do you know why's that? – JvW Aug 02 '22 at 21:58
  • I would guess that your loop is probably assigning to a variable outside the loop, so it always has the value of the last loop iteration. I would declare a `found` boolean, and update it to true within the loop if email&password match, then break. After the loop finishes you can read this variable with your `if`. A better design would be to have two `if`, first to check email, then password. If email is found and wrong, you can exit loop immediately (an email can't have two passwords). A better design again would be to have the dictionary key be email addresses rather than numbers. – RJFalconer Aug 03 '22 at 14:45

1 Answers1

0

Your statement

for key, info in loginInformation.items():

is looping through for each set of elements in loginInformation

First time, it uses the elements

1: {"firstName": "Bert", "lastName": "de Vries", "emailAdress": "bert@gmail.com", "password": "volkswagen"}

That doesn't match, so it displays

Your password and email adress are wrong.

Same thing on the second time through, using

2: {"firstName": "Kristina", "lastName": "van Goor", "emailAdress": "kris94@hotmail.com", "password": "feyenoord"}

The third time through, though, you're processing

3: {"firstName": "John", "lastName": "Doe", "emailAdress": "johndoe@gmail.com", "password": "paashaas"},

And this time, the email address and password matches, so it prints

Hello John Doe , you have successfully logged in.

But then it goes on and processes the fourth entry, too, so you'll probably also see another "Your password and email adress are wrong."

codingatty
  • 2,026
  • 1
  • 23
  • 32
  • Thanks for your clear explanation! I see indeed a fourth entry, unless I use a ````break```` statement. Do you think there's any possibility to only show when the email and password matches? And to ignore the rest of the keys that don't match? – JvW Aug 02 '22 at 21:47
  • My approach (and I recognize this is a learning program, not intended to provide real secure password protection) would be to first input the email address and password. Only when you have that info do you want to look through the entries until you find the matching email address. Then, once you find it, you compare password text. if either of those conditions fail (i.e. if the email address is never found, or if the email address is found, but the password text does not match) , you want to produce the error message, otherwise acknowledge the match. – codingatty Aug 02 '22 at 23:48