Home > database >  Is it a bad practice to call a function recursively on error
Is it a bad practice to call a function recursively on error

Time:11-23

So I have simple this function which, on error, is called again by itself until it returns without an error.

def enter_r_h() -> (float, float):
     try:
         r = float(input("\n--> Enter the radius of the cylinder\n"))
         h = float(input("--> Enter the height of the cylinder\n"))
     except ValueError:
         print("--> Values aren't entered correctly\n")
         return enter_r_h()
     return r, h

Is this a poorly written workaround for when the user enters a bad value since theoretically this could cause a stack overflow (is there such a thing in python?) or the RecursionError.

CodePudding user response:

Why not just put it in a while loop so you are not building the stack? Then you can also count the number of tries and kill the program if the user is ignorant.

On the other hand, you are very unlikely to fill the stack with manually entered attempts. The user may die of old age before it is full.

  • Related