I'm very new to Python, so forgive my newbish question. I have the following code:
[a while loop starts]
print 'Input the first data as 10 characters from a-f'
input1 = raw_input()
if not re.match("^[a-f]*$", input1):
print "The only valid inputs are 10-character strings containing letters a-f"
break
else:
[the rest of the script]
If I wanted to, instead of breaking the loop and quitting the program, send the user back to the original prompt until they input valid data, what would I write instead of break?
To go on with the next loop iteration, you can use the
continue
statement.I'd usually factor out the input to a dedicated function:
Slight alternative:
Or, if you wanted to break it out in to a function (this function is overkill for this use, but an entire function for a special case is suboptimal imo):
This method lets you define your validator. You would call it thusly: