Skip to content

Joseph-T-Gordon

A complete log of all recent projects and skills

Menu
  • About me
  • Posts/Projects
  • Resume/Certifications
Menu

Post #49 (Python Programming) – Errors within your code and how to react

Posted on June 21, 2023June 26, 2023 by admin

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2025 Joseph-T-Gordon | Powered by Minimalist Blog WordPress Theme