Hi!
I'm a beginner using Python 2.5
I'm trying to write a module which will peform the function of data entry and error trapping.
If the user enters an integer outside of a pre-defined range or tries to enter an non-integer value, the module will ask to re-enter until a correct response is made.
I want to call the module repeatedly with different sets of parameters for allowable data range from different points within the main program.
I'm sure this is a very common problem, but can't find the exact solution that I'm looking for in any book or on line, and would be very grateful for your help & guidance.
I've attached my latest (buggy, non-working) attempt to illustrate what I'm trying to achieve.
Many thanks in anticipation
Dave
I'm a beginner using Python 2.5
I'm trying to write a module which will peform the function of data entry and error trapping.
If the user enters an integer outside of a pre-defined range or tries to enter an non-integer value, the module will ask to re-enter until a correct response is made.
I want to call the module repeatedly with different sets of parameters for allowable data range from different points within the main program.
I'm sure this is a very common problem, but can't find the exact solution that I'm looking for in any book or on line, and would be very grateful for your help & guidance.
I've attached my latest (buggy, non-working) attempt to illustrate what I'm trying to achieve.
Many thanks in anticipation
Dave
Code:
def getdata(low_limit,high_limit):
"""Ask user to input integer between certain values
If non-integer is entered, or integer is outside of defined range then
Ask user to re-input until correct response is given"""
try:
n=int(input('Please enter integer between',low_limit,' and ',high_limit,' >'))
return int(n)
except (ValueError,TypeError, n<low_limit, n>high_limit):
return "Error - please re-enter"
def main():
getdata(0,10)
print "done"
Comment