I'm pretty new to python, and want to know what would be the best way to handle exceptions with user input. I have two examples that I've come up with during my study
Example 1:
def userInput():
while True:
try:
balance= float(raw_input("What is your balance: "))
break
except ValueError:
print "Please enter valid input!"
continue
try:
interestR= float(raw_input("Please enter interest as decimal: "))
break
except ValueError:
print "Please enter valid input!"
continue
try:
payRate= float(raw_input("Please enter min payment rate as decimal: "))
break
except ValueError:
print "Please enter valid input!"
continue
return balance, interestR, payRate
Example 2:
def userInput():
while True:
try:
balance= float(raw_input("What is your balance: "))
interestR= float(raw_input("Please enter interest as decimal: "))
payRate= float(raw_input("Please enter min payment rate as decimal: "))
break
except ValueError:
print "Please enter valid input!"
continue
return balance, interestR, payRate
I'm asking for three inputs. In the first example, if the user enters invalid information for one of the three inputs, they'll only have to repeat for that specific input. In the second example, if the user enters invalid input in any of the three, they'll have to restart all three inputs. What would be best practice when handling exceptions with user input. Should I have a standalone function for user input? Is there a cleaner way to code this? Sorry for such a basic question.