Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Aaron Elkind
Aaron Elkind
1,393 Points

An actual complete code though?

While creating the error handling with the number of tickets being over the total, we lose the functionality of the error communication for a user entering a string for the number of tickets requested. It returns this"We ran into an issue. invalid literal for int() with base 10: 'five'. Please try again". Can we see a complete code that can handle both errors and not return "invalid literal for int() with base 10: 'five'. " but return something that says "Please use(enter) a whole number." or something like that.

That would be a complete functional program, IMO.

1 Answer

Steven Parker
Steven Parker
231,248 Points

To get a more a "friendly" message than the built-in error text, you could check if the exception string contains the standard text and issue your own instead:

    except ValueError as err:
        # check for the "unfriendly" standard message
        if "base 10" in str(err):
            # then do NOT include the standard text in output
            print("Oh no! That's not a valid entry! Please use(enter) a whole number.")
        else:
            # Include the error text in the output
            print(f"Oh no, we ran into an issue. {err}. Please try again.")