Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't this work?
#1
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."
Reply
#2
Please put your code in Python code tags, you can find help here.
Reply
#3
There you go.
Reply
#4
you should use configparser.

WAY easier :)
Reply
#5
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
Reply
#6
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 930 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,780 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 885 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,705 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,185 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,010 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,700 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,611 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,945 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,795 Jul-17-2021, 04:19 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

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