python - Two versions of my input function, recursion vs while-loop? -
i need have function returns guaranteed input of type float
. implement came recursive way, seconds later realized use while
-loop well.
so, 1 preferred? there reason use 1 on other?
i'm pretty sure should go while
loop due python's lack of tail calls (afaik?), i'm still not 100% confident.
here both functions:
def inputf(prompt=none): try: return float(input(prompt)) except valueerror: return inputf(prompt) def inputf2(prompt=none): while true: try: return float(input(prompt)) except valueerror: pass
i both acceptable, , there no huge advantage on either of them. unlikely (but never know) user fail input number on hundred times recursive function.
if want stick recursion, worried reaching recursion limit can set higher value, extreme case scenario.
import sys sys.setrecursionlimit(10000)
although, go while loop easier understand.
Comments
Post a Comment