Your browser does not support JavaScript!
Please enable JavaScript to use the full functionality of the site. Without JavaScript, you will not be able to expand the content tables in the PD Materials Page and CSP Index Page.
Tracing code and printing the values of variables often identifies errors in short programs. Below are examples of short code segments without compile-time or run-time errors generating the wrong output. The illustrated errors are quite common and the reasons are explained for each case.
Example 1
a = 4
b = 6
average = a + b / 2
print average
The code prints 7, but the student knows it should be 5.
Cause of error: The statement computing the average has no parenthesis. Thus, b / 2 = 3 was computed and was added to a (resulting in 4+ 3 = 7). Correction:
a = 4
b = 6
average = (a + b) / 2
print average
Example 2
# Calculate the area of a triangle
base = 10
height = 5
area = (1 / 2) * base * height
print area
The code prints 0, but the student knows it should be 25.
Cause of error: In Python 2.x, the type of an operand in an expression defines the type of the output. If both operands are integers, the result is an integer. Hence, 1/2 evaluates to 0 and not 0.5. The statement area = (1 / 2) * base * height assigns 0 to variable area. We need to change one of the operands used in the division to a float. Correction:
# Calculate the area of a triangle
base = 10
height = 5
area = (1.0 / 2) * base * height
print area
Example 3
# Case Sensitive
name_list = ['Tom', 'John', 'Mike']
if "john" in name_list:
print 'Yes'
else:
print 'No'
The code prints ‘No’, but the student knows it should be ‘Yes’.
Cause of error: In the statement if “john” in name_list: we typed ‘john’ rather than ‘John’. Python is a case-sensitive language and thus ‘john’ is different from ‘John’. The code does not generate a compile-time error (it just says “john” is not in the list). Students also make this mistake in naming variables (where it often results in a compile-time error). Correction:
name_list = ['Tom', 'John', 'Mike']
if "John" in name_list:
print 'Yes'
else:
print 'No'
Example 4
# Misunderstanding about list index
name_list = ['Tom', 'John', 'Mike']
if name_list[2] == 'John':
print 'Yes'
else:
print 'No'
The code prints ‘No’, but the student knows it should be ‘Yes’.
Cause of error: In the statement if name_list[2] == ‘John’:, the student thought that name_list[2] would give the value ‘John’ which is the second item in the list. However, indices start at 0 and the second item of the list is name_list[1]. Correction:
# No output generated
# This program is designed to compare two numbers and print out the larger one.
num1 = int(raw_input('Please input the first number: '))
num2 = int(raw_input('Please input the second number: '))
if num1 > num2:
print num1
if num2 > num1:
print num2
The code has no output if the two numbers input by the user is equal.
Cause of error: The program did not consider the situation with equal input. This error appears very often when students start learning if statement. Correction:
# This program is designed to compare two numbers and print out the larger one.
num1 = int(raw_input('Please input the first number: '))
num2 = int(raw_input('Please input the second number: '))
if num1 > num2:
print num1
else:
print num2
Example 6
# Nested Loops. Example 5: Find prime numbers.
# Example from http://www.pd4cs.org/example-5-find-prime-numbers/
n = 5
for i in range(2,n+1):
count = 0
for j in range(2,i):
if(i % j ==0):
count = count + 1
if(count == 0):
print i
The code prints 3 5 5 5, but the student knows it should be 2 3 5.
Cause of error: Confusing the action of the inner loop and the outer loop in a nested loop is common. In this example, the second if-statement if(count == 0): was meant to belong to the outer loop, but the student did not use the correct indentation. The indentation lines up with the first if-statement and thus the second if-statement is executed in the inner loop. Correction:
n = 5
for i in range(2,n+1):
count = 0
for j in range(2,i):
if(i % j ==0):
count = count + 1
if(count == 0):
print i
This work is supported by the National Science Foundation under grant number 1502462. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation"