Study Guide

3.2.7 Undefined Variables CodeHS Answers

3.2.7 Undefined Variables CodeHS Answers

The most common answer is:

a = 10
print a
c = 20
print c
b = c
print b
d = a + b
print d
f = 50
e = f + d
print

In Python 3, you need to use parentheses around the objects you want to print. Also, there’s a missing object to print at the last print statement.

Let’s correct your code for Python 3 compatibility and ensure it prints the intended values:

a = 10
print(a)  # Prints 10
c = 20
print(c)  # Prints 20
b = c
print(b)  # Prints 20
d = a + b
print(d)  # Prints 30 (10 + 20)
f = 50
e = f + d
print(e)  # Prints 80 (50 + 30)

This corrected code includes print statements with parentheses and ensures that each variable’s value is correctly printed. The final print statement now correctly prints the value of e.