Python Forum
Something is not working and i have no idea what
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Something is not working and i have no idea what
#1
Hello guys . I'm new here and i'm new to coding so don't be cruel please. Here is my issue : i'm learning lists now and creating simple program which works like log in screen so i've created 2 lists : in the first one we have emails of registered profiles ,in the second one we have passwords . Everything sorted by index , so for the first email in the list ONLY the first password in the other list should be correct .

emails = ['email one', 'email two', 'email three', 'email four']
passwords = [1111, 2222, 3333, 4444]

email = input('Your email ')
while email not in emails:
    print('EMAIL NOT FOUND')
    email = input('Your email ')

password = int(input('Your password '))
while password not in passwords:
    print('PASSWORD NOT FOUND')
    password = int(input('Your password '))

EMAIL_INDEX = emails.index(email)
PASSWORD_INDEX = passwords.index(password)
while PASSWORD_INDEX != EMAIL_INDEX:
    print('YOUR PASSWORD INCORRECT')
    password = int(input('Your password '))
So the logic of this code is simple , firstly user has to write his email if the email in the list than user can write his password , while it's not in the list user has to write it over and over again this part works perfectly but with passwords i have troubles . Let's continue, user writes password ,while it's not in the list user has to write it again , but next part is kinda tricky . If user writes password FOR THE FIRST TIME which has the same index as email he had written before , than everything is working fine and password recognized... , but if you try to write some random password first and only after that you write correct one you still get 'YOUR PASSWORD INCORRECT' message over and over again ....
I'm sure i've messed up somewhere but i don't get any errors to google where exactly so i'm writing here ... please help
Thanks
Reply
#2
probably there is some problem with the indentation and it's not like what you show here. Your code works as expected for me

Output:
Your email email two Your password 555 PASSWORD NOT FOUND Your password 2222
That said, this is not optimal approach. better use dict, instead of two lists.
Also, I understand this is just for learning purposes, but let say that asking for email/password till you get one that is in the database is not what you would do in real world.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Aug-03-2021, 07:23 AM)buran Wrote: probably there is some problem with the indentation and it's not like what you show here. Your code works as expected for me

Output:
Your email email two Your password 555 PASSWORD NOT FOUND Your password 2222
That said, this is not optimal approach. better use dict, instead of two lists.
Also, I understand this is just for learning purposes, but let say that asking for email/password till you get one that is in the database is not what you would do in real world.

Yes , you wrote password 555 which is not in the list , so when you wrote 2222 it worked fine . But try to write for example password 1111 or any other which is IN the list firstly and then 2222 (or any other depends on your email) and you'll probably see what i'm talking about .
Maybe dict would be better , but for now i have no idea how to use them . I'm a simple man , i've learned something > i try to code something using what i just learned .
anyway ... Thank You for reply .
Reply
#4
(Aug-03-2021, 07:45 AM)RavenSenin Wrote: But try to write for example password 1111 or any other which is IN the list firstly and then 2222 (or any other depends on your email) and you'll probably see what i'm talking about .
Of course if you write a password that is in the list and it does not match the email, you will get Password Incorrect message. You will never get asked for a second time to write a password.

Your description of the problem was different
(Aug-03-2021, 07:03 AM)RavenSenin Wrote: If user writes password FOR THE FIRST TIME which has the same index as email he had written before , than everything is working fine and password recognized... , but if you try to write some random password first and only after that you write correct one you still get 'YOUR PASSWORD INCORRECT' message over and over again ....

Note, the bold is mine and that is what I did - first a random password, not in list, then correct password.

Again, if the password is in the list and does not match the email (i.e. same index for both email and password) then OF COURSE password is INCORRECT for given email even if in the list.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Aug-03-2021, 08:06 AM)buran Wrote:
(Aug-03-2021, 07:45 AM)RavenSenin Wrote: But try to write for example password 1111 or any other which is IN the list firstly and then 2222 (or any other depends on your email) and you'll probably see what i'm talking about .
Of course if you write a password that is in the list and it does not match the email, you will get Password Incorrect message. You will never get asked for a second time to write a password.

Your description of the problem was different
(Aug-03-2021, 07:03 AM)RavenSenin Wrote: If user writes password FOR THE FIRST TIME which has the same index as email he had written before , than everything is working fine and password recognized... , but if you try to write some random password first and only after that you write correct one you still get 'YOUR PASSWORD INCORRECT' message over and over again ....

Note, the bold is mine and that is what I did - first a random password, not in list, then correct password.

Again, if the password is in the list and does not match the email (i.e. same index for both email and password) then OF COURSE password is INCORRECT for given email even if in the list.

Okay I'm sorry that i was not clear enough ... by ''random password'' i meant password which is IN the list BUT not correct one .Because if you write password which is NOT IN the list you get into another loop
while password not in passwords:
    print('PASSWORD NOT FOUND')
    password = int(input('Your password '))
The main problem appears when your FIRST written password is IN the list but has different index with email . Here how it looks like
Output:
Your email email one Your password 2222 YOUR PASSWORD INCORRECT Your password 1111 YOUR PASSWORD INCORRECT Your password 1111 YOUR PASSWORD INCORRECT Your password 1111 YOUR PASSWORD INCORRECT Your password 1111 YOUR PASSWORD INCORRECT
As you can see after 'YOUR PASSWORD INCORRECT' message , program asks you for password again , but even if you write correct one it still says that password incorrect . The question is WHY ?
Thanks
Reply
#6
Quote:The question is WHY ?

The answer to the question is actually simple ... Instead of :
EMAIL_INDEX = emails.index(email)
PASSWORD_INDEX = passwords.index(password)
while PASSWORD_INDEX != EMAIL_INDEX:
    print('YOUR PASSWORD INCORRECT')
    password = int(input('Your password '))
I've tried to write:

EMAIL_INDEX = emails.index(email)
PASSWORD_INDEX = passwords.index(password)
while PASSWORD_INDEX != EMAIL_INDEX:
    print('YOUR PASSWORD INCORRECT')
    password = int(input('Your password '))
    EMAIL_INDEX = emails.index(email)
    PASSWORD_INDEX = passwords.index(password)
And it worked , now i can write wrong password as long as i want and once password is correct the loop is finished. So basically i use the same code two times and well ... yeah it doesn't look good to be honest , but as long as it works i'm happy. It still shows an error though ... if after the password which is IN the list but not correct you try to write password which IS NOT in the list , but i will solve it ... somehow
Reply
#7
(Aug-03-2021, 07:03 AM)RavenSenin Wrote: Hello guys . I'm new here and i'm new to coding so don't be cruel please. Here is my issue : i'm learning lists now and creating simple program which works like log in screen so i've created 2 lists : in the first one we have emails of registered profiles ,in the second one we have passwords . Everything sorted by index , so for the first email in the list ONLY the first password in the other list should be correct .

emails = ['email one', 'email two', 'email three', 'email four']
passwords = [1111, 2222, 3333, 4444]

email = input('Your email ')
while email not in emails:
    print('EMAIL NOT FOUND')
    email = input('Your email ')

password = int(input('Your password '))
while password not in passwords:
    print('PASSWORD NOT FOUND')
    password = int(input('Your password '))

EMAIL_INDEX = emails.index(email)
PASSWORD_INDEX = passwords.index(password)
while PASSWORD_INDEX != EMAIL_INDEX:
    print('YOUR PASSWORD INCORRECT')
    password = int(input('Your password '))
So the logic of this code is simple , firstly user has to write his email if the email in the list than user can write his password , while it's not in the list user has to write it over and over again this part works perfectly but with passwords i have troubles . Let's continue, user writes password ,while it's not in the list user has to write it again , but next part is kinda tricky . If user writes password FOR THE FIRST TIME which has the same index as email he had written before , than everything is working fine and password recognized... , but if you try to write some random password first and only after that you write correct one you still get 'YOUR PASSWORD INCORRECT' message over and over again ....
I'm sure i've messed up somewhere but i don't get any errors to google where exactly so i'm writing here ... please help
Thanks

In the lines
 while PASSWORD_INDEX != EMAIL_INDEX:
    print('YOUR PASSWORD INCORRECT')
    password = int(input('Your password '))
you do not change the PASSWORD_INDEX after reading the new password, so no matter what you type, the unchanged PASSWORD_INDEX is always not equal to the EMAIL_INDEX, so the loop keeps repeating.

The code should be
 while PASSWORD_INDEX != EMAIL_INDEX:
    print('YOUR PASSWORD INCORRECT')
    password = int(input('Your password '))
    PASSWORD_INDEX = passwords.index(password)
Reply
#8
Unrelated to your question, but why are you making passwords a number? Unless you need something to be a number it is best to leave it a string. You don't have to worry about your program crashing when the user enters a password that isn't a number.

If you want to relate something like email addresses and passwords, use a dictionary. If you need to relate several pieces of information use a class. Splitting related information into separate data structures (like two lists) is just asking for trouble. Eventually you will make an error maintaining the lists and the bug will be difficult to trace.

I also like using the new walrus operator for something like this:
emails = {
    'email one':'1111',
    'email two':'2222',
    'email three':'3333',
    'email four':'4444'}

while not (email := input('Your email ')) in emails:
    print('EMAIL NOT FOUND')

while (password := input('Your password ')) != emails[email]:
    print('PASSWORD NOT FOUND')
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020