I am trying to catch an input error - where the only valid input is an integer.
If a non-integer is entered, I want it to go back to the beginning of func and try again:
def setItorator():
try:
iterationCap = int(raw_input("Please enter the number of nibbles you want to make: "))
print "You will make", iterationCap, "nibbles per file"
except ValueError:
print "You did not enter a valid integer, please try again"
setItorator()
return iterationCap
if __name__ == "__main__":
iterationCap = setItorator()
This method works if the first input is valid and returns to the beginning of func if invalid input is executed, but it does not seem to return the correct true main function. I checked in sub func that it sees the correct variable, and the correct type (int), and it appears, but I get an error:
UnboundLocalError: local variable 'iterationCap' referenced before assignment
I do not see this error if the first input is valid (for example, "10"), only if the first input is invalid (for example, "a" and then "10")
source
share