Validating user input against text file

Reply

Join Date: Nov 2008
Posts: 2
Reputation: myself2211 is an unknown quantity at this point 
Solved Threads: 0
myself2211 myself2211 is offline Offline
Newbie Poster

Validating user input against text file

 
0
  #1
Nov 10th, 2008
Hi, just beginning to learn Python, and am trying to create a script that will output matches from a text file to a user's input, I have managed that so far, the problem I have is trying to have something returned if the user puts in a string of text that does not match what is in the text file. ie - number not found. I have tried 'else', but it didn't work, I think I had put it in the wrong place.

Here's where I am at -

  1. print"\nSEARCHING NUMBERS"
  2.  
  3. print "\nPlease Enter the Room Number 1, 2, 3, 4 or 5."
  4.  
  5. text_file = open("read_it.txt", "r")
  6.  
  7. word = raw_input("Type the Number you want to check: ")
  8. word = word.upper()
  9.  
  10. print "\nnumber."
  11. for line in text_file:
  12.  
  13. # if the line does not contain the typed word
  14. # then continue to the next line
  15.  
  16. if ''.join(line).find(word) == -1:continue
  17. print line
  18. print
  19.  
  20. if ''.join(line).find(word) != 1:
  21. print line
  22. print
  23.  
  24. for line in text_file:
  25. print line
  26.  
  27. raw_input("\n\nPress the enter key to exit.")
  28.  
  29. text_file.close()

Any help/suggestions greatly appreciated.

M
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Validating user input against text file

 
0
  #2
Nov 10th, 2008
If I understand the problem, you want an else on the for loop, and do a break when you don't want it executed. Try something along these lines:
  1. for line in text_file:
  2.  
  3. # if the line does not contain the typed word
  4. # then continue to the next line
  5.  
  6. if ''.join(line).find(word) == -1:continue
  7. print line
  8. break
  9. else:
  10. print "Not found" # Executed whenever break above is NOT executed
  11. print
If you can't do the break at that spot in the code (e.g., because you need to go thru the entire loop, not just stop at the first match) then you'll have to do a little more work, but you should be able to figure that out .
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: myself2211 is an unknown quantity at this point 
Solved Threads: 0
myself2211 myself2211 is offline Offline
Newbie Poster

Re: Validating user input against text file

 
0
  #3
Nov 10th, 2008
Thanks BearofNH, now giving it ago.
M

Originally Posted by BearofNH View Post
If I understand the problem, you want an else on the for loop, and do a break when you don't want it executed. Try something along these lines:
  1. for line in text_file:
  2.  
  3. # if the line does not contain the typed word
  4. # then continue to the next line
  5.  
  6. if ''.join(line).find(word) == -1:continue
  7. print line
  8. break
  9. else:
  10. print "Not found" # Executed whenever break above is NOT executed
  11. print
If you can't do the break at that spot in the code (e.g., because you need to go thru the entire loop, not just stop at the first match) then you'll have to do a little more work, but you should be able to figure that out .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC