By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

Search txt file for user input.

P: 3
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.

Expand|Select|Wrap|Line Numbers
  1. print"\nSEARCHING NUMBERS"  
  3. print "\nPlease Enter the Number 1, 2, 3, 4 or 5."  
  5. text_file = open("read_it.txt", "r")  
  7. word = raw_input("Type the Number you want to check: ") word = word.upper()  
  9. print "\nnumber." 
  10. for lines in text_file:  
  12. # if the line does not contain the typed number 
  13. # then continue to the next line      
  15. if ''.join(lines).find(word) == -1:continue         
  16. print lines 
  18. break 
  19. else:        
  21. print "Not found"     # Executed whenever break above is NOT executed 
  22. print  raw_input("\n\nPress the enter key to exit.")  
  24. text_file.close()  
I am only a beginner, so would appreciate any help or suggestions thank you.
L :)
Nov 12 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 469
Expand|Select|Wrap|Line Numbers
  1. # if the line does not contain the typed number
  2. # then continue to the next line
  3. if ''.join(lines).find(word) == -1:continue
  4. print lines
  5. break
  6. else:
  7.     print "Not found"     # Executed whenever break above is NOT executed
This code is confused. I don't know which code is supposed to be in the if block. A continue jumps directly up to the top of the loop. There's no point in doing anything like breaking or printing anything after you have used continue. For now, you don't have to use continue or break; a simple if-else should do it.
Expand|Select|Wrap|Line Numbers
  1. if the word is found:
  2.     print "Found"
  3. else:
  4.     print "Not found"
As for checking if the word is found, the find function is not nessecary. just use the in operator:
Expand|Select|Wrap|Line Numbers
  1. if word in lines:
  2.     print "Found"
Hope this helps.
Nov 12 '08 #2

P: 3
thanks Boxfish, I am continuously getting an "invalid syntax"
Expand|Select|Wrap|Line Numbers
  1. if the 'word' is found:

I appreciate your feedback and can see the confusion, (it's a true reflection of the writer).

L :(
Nov 12 '08 #3

Expert 100+
P: 469
Sorry, that was pseudocode. Use the second bit of code that uses the in operator to find the word.
The more you program, the less confused you will be.
Nov 12 '08 #4

P: 3
Thank you boxfish.
Nov 13 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.