| | |
Validating user input against text file
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
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 -
Any help/suggestions greatly appreciated.
M
Here's where I am at -
Python Syntax (Toggle Plain Text)
print"\nSEARCHING NUMBERS" print "\nPlease Enter the Room 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 line in text_file: # if the line does not contain the typed word # then continue to the next line if ''.join(line).find(word) == -1:continue print line if ''.join(line).find(word) != 1: print line for line in text_file: print line raw_input("\n\nPress the enter key to exit.") text_file.close()
Any help/suggestions greatly appreciated.
M
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: 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
.
Python Syntax (Toggle Plain Text)
for line in text_file: # if the line does not contain the typed word # then continue to the next line if ''.join(line).find(word) == -1:continue print line break else: print "Not found" # Executed whenever break above is NOT executed
. •
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Thanks BearofNH, now giving it ago.
M
M
•
•
•
•
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: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 outPython Syntax (Toggle Plain Text)
for line in text_file: # if the line does not contain the typed word # then continue to the next line if ''.join(line).find(word) == -1:continue print line break else: print "Not found" # Executed whenever break above is NOT executed.
![]() |
Similar Threads
- Password Storage System. (Python)
Other Threads in the Python Forum
- Previous Thread: Gene Sequence Problem
- Next Thread: sending information with a wsdl file
| Thread Tools | Search this Thread |
address aliased anydbm app beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary dynamic events excel external feet file float format function generator getvalue handling homework iframe images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse number numbers output parsing path port prime programming py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile signal singleton slicenotation split string strings tails text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web whileloop word wxpython xlwt





