Hi, this is my first attempt at Python, what I am trying to achieve is a script that will ask the user for input, search a txt file and display the output or give not found message. Ideally I would like it to ask the user once the first output is achieved for further user input if required, otherwise exit the program. However, would be happy if it achieved the first objective.
Following is as far as I can go, the problem is it is breaking after the first find, whereabouts I know there are additional matches in the txt file. I think I might need to replace the IF with a WHILE, which I have tried but keep getting errors, and am unable to work it out.
I am only a beginner, so would appreciate any help or suggestions thank you.
L :)
Following is as far as I can go, the problem is it is breaking after the first find, whereabouts I know there are additional matches in the txt file. I think I might need to replace the IF with a WHILE, which I have tried but keep getting errors, and am unable to work it out.
Code:
print"\nSEARCHING NUMBERS"
print "\nPlease Enter the Number 1, 2, 3, 4 or 5."
text_file = open("read_it.txt", "r")
word = raw_input("Type the Number you want to check: ") word = word.upper()
print "\nnumber."
for lines in text_file:
# if the line does not contain the typed number
# then continue to the next line
if ''.join(lines).find(word) == -1:continue
print lines
break
else:
print "Not found" # Executed whenever break above is NOT executed
print raw_input("\n\nPress the enter key to exit.")
text_file.close()
L :)
Comment