Python Forum

Full Version: Variable from notepad problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello dear users, let me explain you my problem with variable.

b = open("test.txt", "r").readline()

a = input("Write")

if a == b:
print("good")

I have really simple program like this. In my "test.txt" i have only two lines and in first is "a" and in second "b"

If i put in my input "a" this if statement not work and not print "good". But when only try to print out print(b) it prints "a" so i do not understand why it is not working.

My variable b is equal to "a" but my if statement says it is not equal.

I also noticed when i have only one line in my notepad then it is working. So please guys can you help me with this ? Why it is not working when i have two lines in notepad.

Thank you a lot !
Please format your code according to the rules. Please read BBcode to learn how to do this.
I think your problem is in extra characters. readline() will also read the newline at the end of a line. You should strip() what you get from readline(). Perhaps this works:
b = open("test.txt", "r").readline().strip()
THANK YOU A LOT !!!