Advanced âĸ Python
Fixing Errors
đ Reading Error Messages
When Python finds a problem, it stops and shows an error message. Don't worry, errors are clues, not failures! Look at the last line first: it tells you the type of error and what went wrong. A NameError means Python does not know a name, often because of a typo or a variable that was never made.
print("Start!")
print(score)Start! NameError: name 'score' is not defined
đ Everything in Fixing Errors
Every programmer makes mistakes! Learn to read error messages like a detective, fix common bugs, and use try and except so your programs don't crash. Here is every step in this topic in one place, so you can review what you learned or jump straight to a step.
đ Reading Error Messages
When Python finds a problem, it stops and shows an error message. Don't worry, errors are clues, not failures! Look at the last line first: it tells you the type of error and what went wrong. A NameError means Python does not know a name, often because of a typo or a variable that was never made.
print("Start!") print(score)âī¸ SyntaxError: Typos in Code
A SyntaxError means the code is not written the way Python expects, like a sentence with missing punctuation. Common causes are a missing bracket, a missing quotation mark, or a missing colon after if or for. With a SyntaxError, Python won't run any of the program until it is fixed.
print("Hello"đ§Š TypeError and ValueError
A TypeError happens when we mix types that don't fit together, like adding text and a number. A ValueError happens when the type is right but the value is wrong, like int("seven"), because Python can't turn the word seven into a number. We can fix a TypeError by changing the number into text with str().
age = 10 print("I am " + str(age) + " years old") print("I am " + age + " years old")đĨ Catching Errors with try and except
We can stop a program from crashing by putting risky code inside try. If an error happens, Python jumps to the except part instead of stopping. We can name the error we want to catch, like ZeroDivisionError or ValueError.
try: print(10 / 0) except ZeroDivisionError: print("Oops! You can't divide by zero.") try: number = int("seven") except ValueError: print("That is not a number!") print("The program keeps going!")đ else and finally
try can have two extra helpers. The else part runs only if there was no error. The finally part always runs at the end, error or no error, so it is great for saying goodbye or cleaning up.
try: result = 20 / 4 except ZeroDivisionError: print("Can't divide by zero!") else: print("The answer is", result) finally: print("All done!")đĩī¸ Debugging Tips
Debugging means finding and fixing bugs. A great trick is to add extra print() lines to see what your variables hold while the program runs. Also check spelling, brackets, colons, and indentation, and fix one error at a time.
total = 0 for number in [2, 4, 6]: total = total + number print("DEBUG: number is", number, "and total is", total) print("Final total:", total)
