Debugging Code
Computers are very picky about what they run and don't run. A computer wants everything to be exact, which can lead to errors. Let's learn how to throw those errors away...
Computers are very picky about what they run and don't run. A computer wants everything to be exact, which can lead to errors. Let's learn how to throw those errors away...
There are a few common mistakes in Python. Let's see what they are, and how to fix them.
What's wrong with the code below?
from turtle import *
t = Pen()
s = Screen()
T.forward(50)
Do you see that instead of t
, we used T
? In Python, variable names are case-sensitive, meaning that the uppercase / lowercase values are different, so make sure that you use the same case in variable / function / property / method names. This error can be found in EVERY programming language, so check your code for undefined things.
Python is a good language for beginners because it does not involve lots of complex punctuation. However, sometimes you can forget punctuation. What's wrong with the code below?
from turtle import
t = Pen()
s = Screen()
t.up(
t.forward(50)
t.down()
t.circle(50)
You can see the the from turtle import
line is missing the *
. That will confuse Python and produce an error. Also, the t.up(
line is missing a closing parenthesis. All languages have some form of punctuation, meaning that you should check your punctuation when going over code.
This error is like missing punctuation, but the opposite. Look at this code:
from turtle * import *
t == Pen()
s = Screen()
t.forward(50)
Did you see that in line 1, we put a second asterisk (*
), and that line 2 has an extra equals sign (==
)? The first line produces a straight-up error, while the second line checks if the undefined variable t
is equal to Turtle()
. This will also produce an error. Like with the previous bugs, this one is common to all languages, and you should always check your code for extra punctuation.
What wrong with this?
from turtle imprt *
t = Pen()
s = Screen()
t.forwrd(50)
First of all, imprt
is spelled incorrectly, which will produce an error. Second of all, forwrd
is also spelled incorrectly, which would also produce an error. Just like with mistyped variables and missing punctuation, this error is found in every programming language. Make sure to check your work for mistyped things.
This error is NOT common to all languages, and in fact is only found in a few, including Python. This is because not many coding languages rely on indentation to work. Take a look at this code:
age = int(float(input("Age:")))
if age < 10:
print("You are less than 10 years old!")
print("That's cool!")
elif age < 20:
print("You are less that 20 years old!")
print("That's cool!")
else:
print("You are older than 20.")
Lines 3 and 4 should have the same level of indentation, but they don't. This causes Python to think that there should have been an if:
or a for:
block there, but there wasn't. This causes Python to produce an error.
Also, the else
statement is inside the elif
statement, where it shouldn't be. Python received that else
statement without getting an if
or elif
statement inside the same block, causing it to produce an error.
What does this code do?
from turtle import *
t = Pen()
s = Screen()
t.up(10)
t.forward(50)
t.down()
t.circle()
Answer: it produces an error! First of all, on line 4, we call t.up()
WITH a parameter, which we're not supposed to do. The .up()
method takes zero parameters, not one!
Second of all, the t.circle()
function takes one parameter, but we gave it none. This also produces an error. Parameter errors are found in all languages, and you should be careful of them.
In this challenge, you have to fix the code given. There are several bugs. Try to find them all. When you're done, click "Show Answers" to see the answers. Changes will be highlighted in bold.
You may want to copy/paste this code into a text editor to make it easier to change it.
from turtle import *
t = Pen
s = Screen()
def number(num):
return int(float(num)
class startFill:
def __init__(self,color):
self.color = color
def start(self):
t.color(self)
t.begin_fill()
def end():
t.end_fill()
t.color("red")
t.up
t.forward(10)
t.down()
for i ["red","yellow","green","blue"]:
t.color(i)
t.begin_fill()
t.circle()
t.end_fill()
t.left(90)
t.up
t.backward(10)
t.seth(270)
t.forward
t.down()
length = input("Go forward ____ pixels:").number()
t.forward(length)
color = input("Color:")
startFill(color).start
t.circle(50)
startFill(color).end()
print("This program finished!)
from turtle import *
t = Pen() # Changed from "Pen". Missing parentheses.
s = Screen()
def number(num):
return int(float(num)) # Changed from "int(float(num)" Missing closing parenthesis.
class startFill:
def __init__(self,color):
self.color = color
def start(self):
t.color(self.color) # Changed from "self". Trying to get self.color, not entire startFill object.
t.begin_fill()
def end(self): # Changed from "end()". All functions within a class must have at least one parameter.
t.end_fill()
t.color("red")
t.up() # Changed from "t.up". .up() is a method, not a property.
t.forward(10)
t.down()
for i in ["red","yellow","green","blue"]: # Changed from "". A for loop must look like "for var in iterable object".
t.color(i)
t.begin_fill()
t.circle(50) # Changed from t.circle(). .circle() takes one parameter.
t.end_fill()
t.left(90)
t.up() # Changed from "t.up". .up() is a method, not a property.
t.backward(10)
t.seth(270)
t.forward(50) # Changed from "t.forward". .forward() is a method, not a property.
t.down()
length = number(input("Go forward ____ pixels:")) # Changed from "input('Go forward ____ pixels:').number()". number() is a function, not a method of input().
t.forward(length)
color = input("Color:")
startFill(color).start() # Changed from "start". .start() is a method, not a property.
t.circle(50)
startFill(color).end()
print("This program finished!") # Changed from "!)". Missing double quotes.