Python Forum

Full Version: Why doesn't this work?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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."
Please put your code in Python code tags, you can find help here.
There you go.
you should use configparser.

WAY easier :)
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
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]