📢
2
c/coding-for-beginners•the_paulthe_paul•11d ago

PSA: I found a way to stop my code from crashing when I type the wrong thing

I kept trying to make a simple number guessing game in Python, but it would crash every time someone typed a word instead of a number. I felt stuck for a whole day. Then I watched a video from a guy named Corey Schafer on YouTube, and he showed a trick with try and except. You put the input line inside a try block, and if someone types something that isn't a number, the except block catches it and asks them to try again. I tried it and it worked on the first go! My game just loops back now instead of showing that scary error message. It made me realize that planning for mistakes is a big part of writing good code. Has anyone else found a different way to handle this kind of user input error?
2 comments

Log in to join the discussion

Log In
2 Comments
bethcarr
bethcarr11d agoOG Member
Wow, that's basically error handling 101.
7
gibson.oliver
gibson.oliver11d agoMost Upvoted
Remember that article about how error messages can make or break user trust? It argued that clear error handling is less about code and more about communication. Makes you wonder why so many apps still get it wrong.
5