Python Forum
Why doesn't this work? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why doesn't this work? (/thread-9561.html)



Why doesn't this work? - Saladrang - Apr-16-2018

import time
file = open("login.txt","r")
TrueUsername = file.readline(1)
TruePassword = file.readline(2)
print("Log in")
time.sleep(0.5)
Username1 = input("Username:")
Password1 = input("Password:")

if Username1 == TrueUsername and Password1 == TruePassword:
    print("Welcome.")   
else:
    print("Try again.")
file.close()
---Whenever I write the correct password and username on the .txt file it prints "Try again."


RE: Why doesn't this work? - j.crater - Apr-16-2018

Please put your code in Python code tags, you can find help here.


RE: Why doesn't this work? - Saladrang - Apr-16-2018

There you go.


RE: Why doesn't this work? - vervoortraf - Apr-16-2018

you should use configparser.

WAY easier :)


RE: Why doesn't this work? - woooee - Apr-16-2018

If you print your variables, you will find that the variables created by readline() contain a newline at the end, and the variables created by input do not, so they will never be equal. https://www.tutorialspoint.com/python/file_readline.htm


RE: Why doesn't this work? - IAMK - Apr-17-2018

Hence, going off woooee's comment, you need to split the readline to remove the newline.
Try something like: TrueUsername = file.readline(1).split('\n')[0]
If ^ doesn't work, then do:
TrueUsername = file.readline(1).split('\n')
if Username1 == TrueUsername[0]