Python Forum
Variable from notepad problem - 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: Variable from notepad problem (/thread-25736.html)



Variable from notepad problem - samuelbachorik - Apr-10-2020

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 !


RE: Variable from notepad problem - ibreeden - Apr-10-2020

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()



RE: Variable from notepad problem - samuelbachorik - Apr-10-2020

THANK YOU A LOT !!!