If you’re reading this you’ve probably seen errors in your code before, here’s the question what do you? Here is am example of code that will return an error
Ex:
var = int(input(“Enter an integer here: “))
print(var)
Result:
Enter an integer here: No
Traceback (most recent call last): File "main.py", line 1, in var = int(input("Enter an integer here: ")) ValueError: invalid literal for int() with base 10: 'No'
As you can see this error could’ve been easily avoided if the user had just followed the rules but you need to be prepared for that, for example you could’ve used is to check if the type was integer.
type(var) is int
I have a better way. Let me show you try and except. The try is where you put your code that could potentially have issues and instead of blurting confusing errors on the screen it will move to the except side where you can run your response.
Ex:
try:
var = int(input(“Enter an integer here: “))
print(var)
except:
print(“woah buddy I asked for an integer”)
Result:
Enter an integer here: No
woah buddy I asked for an integer
As you can see you can use try and except to have a more user friendly error response for the more error prone sections of your code. The except likely won’t even be triggered most of the time it only occurs if you encounter an exception, an error.