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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -