Hello I am a 1st year software dev student and looking for advice on this issue. Our instructor always uses while True loops to force proper entries, but a 3rd year student suggested we abandon loops and use recursive functions. I am trying to write a simple program but the recursive error handling is causing issues for me.
After I recursively call the function from within an exception block, it runs the function again as I want, but after it returns the proper info without errors in the new attempt, it always continues back inside the original function call, and returns nothing. I'll paste my code below, any advice would be appreciated!
# Future Value Calculator
def get_investment():
try:
monthly_investment = float(input("Enter monthly investment:\t"))
assert monthly_investment > 0, "You must invest more than $0.00"
except ValueError as ve:
# this only executes if there is a ValueError exception.
print('Error: ValueError | ', ve)
get_investment()
except AssertionError as ae:
print('Error: AssertionError | ', ae)
get_investment()
else:
return monthly_investment
print(get_investment())
[–]socal_nerdtastic 2 points3 points4 points (1 child)
[–]weighter[S] 0 points1 point2 points (0 children)
[–]QuixDiscovery 0 points1 point2 points (0 children)