Python Python Basics All Together Now Handle Exceptions

Why raise exception rather than using a simple loop?

I approached the problem of user inputting letters rather than digits with the following code immediately before the line converting the input to an integer:

while not number_tickets.isdigit():
    number_tickets = input("Enter only numeric digits:  ")

That worked fine and didn't force the user to enter their name again. What is the advantage of using the "raise ValueError" approach instead?

1 Answer

Steven Parker
Steven Parker
186,664 Points

I doubt most of the course examples are meant to be taken as the best or only strategy for addressing a certain need. They are just picked as ways to illustrate the use of the technique being introduced.

The fact that you're thinking of effective alternative strategies is evidence of your learning progress and grasp of the material. Good job! :+1: