Search txt file for user input.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • myself2211
    New Member
    • Aug 2008
    • 3

    Search txt file for user input.

    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.

    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()
    I am only a beginner, so would appreciate any help or suggestions thank you.
    L :)
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Code:
    # 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
    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.
    Code:
    if the word is found:
        print "Found"
    else:
        print "Not found"
    As for checking if the word is found, the find function is not nessecary. just use the in operator:
    Code:
    if word in lines:
        print "Found"
    Hope this helps.

    Comment

    • myself2211
      New Member
      • Aug 2008
      • 3

      #3
      thanks Boxfish, I am continuously getting an "invalid syntax"
      Code:
      if the 'word' is found:
      .

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

      L :(

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        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.

        Comment

        • myself2211
          New Member
          • Aug 2008
          • 3

          #5
          Thank you boxfish.
          :)

          Comment

          Working...