4

I am doing a python course where they suggested a try and except block in a while loop in order to keep asking for input until the condition is satisfied. Intuitively I feel it is shorter to just call the function again in the "except" block like this:

def exceptiontest():
    try:
        print(int(input("number 1: "))+int(input("number 2:")))
    except:
        print("a mistake happened")
        exceptiontest()

exceptiontest()

When asking on the forum on the course I got the reply that it is not the same. I am a bit confused now. Anyone that can clarify for me? Thanks in advance!

Dutchpirate91
  • 109
  • 1
  • 5
  • 1
    you are stapling function calls on the call stack : `exceptiontest(exceptiontest(exceptiontest(exceptiontest(exceptiontest(exceptiontest(exceptiontest())))))):` if you call the function again and again. thats unneded efford and bad practice - a while loop does not need to do this because you stay in the same function. – Patrick Artner Oct 12 '18 at 13:50
  • Read: [How to ask the user for input until valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Patrick Artner Oct 12 '18 at 13:51
  • [relevant cross-site question](https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why) –  Oct 12 '18 at 13:53
  • Dont let errors pass silently. It's good to use: except ValueError as e: print(e). – Stef van der Zon Oct 12 '18 at 13:54
  • @Patrick Artner, very useful link, thank you. I understand that it has to do with performance and the call stack. Great help! – Dutchpirate91 Oct 12 '18 at 17:48

3 Answers3

5

Calling the function in the except will eventually raise a RecursionError: maximum recursion depth exceeded error if you keep entering bad inputs. Generally must humans won't be entering that many bad data to hit the error before they give up, but you are unnecessarily putting function calls on a stack.

A while loop is better since it's one function call, waiting for a valid input. IT doesn't waste any more resources than it needs.

MooingRawr
  • 4,901
  • 3
  • 24
  • 31
2

while loop, for two reasons

  • it's clearer to read: while not success, try again
  • recursion is not free. It leaves the previous function stack open. it could run out of memory (probably won't, in this case, but in principle, avoid it)
blue_note
  • 27,712
  • 9
  • 72
  • 90
1

Another reason to use the while loop which has not yet been mentioned is that you could leverage the assignment expressions coming with Python 3.8.

The function add encapsulates getting two numbers and trying to add them.

def add():
    'try to add two numbers from user input, return None on failure'
    x = input('number 1: ')
    y = input('number 2: ')
    try:
        return float(x) + float(y)
    except TypeError, ValueError:
        return None

The following while loop runs as long as there is no result.

while (result := add()) is None:
    print('you made a mistake, make sure to input two numbers!')

# use result
timgeb
  • 76,762
  • 20
  • 123
  • 145