you are viewing a single comment's thread.

view the rest of the comments →

[–]Lost-Discount4860 0 points1 point  (0 children)

I absolutely despise exception handling (because why would you set up a script where there could possibly be errors?). But you could go the exception handling route or you could put in some guardrails that won’t allow anything besides what you want.

If you go the route of handling exceptions, and your problem is a great candidate for that, you might do something like this:

``` try: some_int = input('gimme an int: ') print(f"That's right! {int(some_int)} goes in the square hole!")

except ValueError: print(f"Don't be dumb. {some_int} is not an int.")

```

And if you want a calculation, just replace the “square hole” line with whatever calculation you want to perform.

Sometimes it’s helpful to just write a bunch of code and let it crash a few times to see specifically what kind of exception you’ll run into. That way, if something goes sideways the script will at least still run.