if your input is john why isn’t the if statement kicking in????
studentname.txt
john 34
paul 37
poop 45
above is whats in studentname.txt
b=a
name = input('students name : ')
list1=[]
file=open('studentname.txt','r')
for (a) in file:
list1.append(a)
b=a[:-3]
why isn’t this next if statement tripping if name entered is ‘john’ for instance??
if name == b:
print(a)
file.close
You are picking up newlines. Depending on the os you created the file on, you’ll have different new line characters. The safest way to rid yourself of this is:
That will take care of any trailing whitespace.
You could also do:
Also, don’t name your variable ‘file’. This is a python built-in function that you’ve now renamed for your script and any script that imports it.
Finally, you might prefer to handle files like this:
No need to close the file, the with statement controls it’s scope and closes it.