I'm trying to learn python and I wrote this script, that not shows me the right output.. What am i missing?
I have a text file like this:
test
test1
test2
test3
test4
#test5
test6
#test7
#test8
TASK 1:
- open file: i got this part
- *do not print or ignore any line that starts with "#": i think i got this part (fread2)
Here is my code:
fopen=open('file1.txt',mode='r+')
fread=fopen.read()
fread2="\n".join([line.strip() for line in fread.splitlines() if not line.startswith('#')])
print(fread2)
fopen.close()
Output:
test
test1
test2
test3
test4
test6
Note: test5, 7 and 8 did not print. Success!
TASK 2:
- ask user to input a text: completed
- from fread2 output - use the user "input" value and if value exit, then print that line only: failed!!
See the following code:
fopen=open('file1.txt',mode='r+')
fread=fopen.read()
fread2="\n".join([line.strip() for line in fread.splitlines() if not line.startswith('#')])
text=input("Enter text: ")
for x in fread2:
if text == "":
continue
if text in x:
print(x)
fopen.close()
Output
Enter text: test
Where I'm wrong?