Python 3.8 > Login Problem

Hi,
I’m a beginner level in Python 3 and as one of my first projects, I decided to create a sign-up screen and login screen.

They are both very simple.
Sign-up screen takes user’s input (username and password) and puts it into a .txt file.

Login screen then should take user’s input and compare it to the .txt file to see if credentials match. For some reason, this just doesn’t work!

The program always DENIES username and password. The problem is not with line numbering. I tried to print the credentials in a test program and they were printed out correctly.

I’m ready to drop this code and move on to better solutions for login screens (this is my first attempt) but first I would like to understand why this doesn’t work.

Thank you!

d

It’s hard to say with this little information. I would imagine however, that you perhaps want

if username in credentials[1] and password in credentials[2]

assuming that credentials[1] is an array of all usernames and credentials[2] is an array of all passwords. Also, are you sure you don’t want credentials[0] and credentials[1], as Python begins indexing at 0?

1 Like

I’m sorry, I forgot that I changed line 5 from database.readlines() to database.read before taking screenshot. It was my desperate attempt to make it work… in line 5 I actually use .readlines, so it looks like this:

It’s really hard to say much unless you can tell me what is actually inside of the variable credentials.

if username in credentials[1] and password in credentials[2]

this WORKED!!!

So for some reason I couldn’t use == to compare the user input and the .txt file lines, but in worked.

May the reason be that ‘username’ variable contains more information than the username ? That would also explain why “in” worked.

Thank you JeremyLT, I cheer to you brother! <3

1 Like

I’m glad I could help. The comparison == only works for single values. In this case it looks like you had arrays and needed to check if username and password were inside of those arrays. In Python, the way to check if val is in array is if val in array.

1 Like