Exercises
Practice the complete workflow from this chapter: interpret prompts and output, choose a verified Python command, read a traceback, correct small programs, distinguish comments from code, and use intermediate values to guide an edit.
These exercises combine the chapter's main skills: reading code and transcripts, choosing a verified command, predicting results, interpreting tracebacks, correcting small programs, and using comments and intermediate output.
The sequence begins with recognition and ends with editable programs. Predict a result before running code when possible, then use the actual result as evidence.
Read Code and Execution Results
Exercise: Separate prompt, code, and displayed value
Read this interactive-session transcript:
>>> value = 6
>>> value + 1
7
Which interpretation is correct?
Select one choice, then check.
HintAsk who supplied each part
The interactive environment displays >>> before input and displays the
value of an expression after evaluating it.
SolutionThe transcript contains three kinds of text
The prompt marker >>> comes from the interactive environment. The reader
entered value = 6 and value + 1. The assignment stores a value without
displaying one; the expression produces the displayed value 7.
Exercise: Run a script with the verified command
On a computer, python3 --version successfully reports Python 3.13. The current
folder contains hello.py. Which command uses that verified interpreter to run
the script?
Select one choice, then check.
HintSeparate checking from running
--version asks Python to report information. Running a script passes the
filename directly to the verified Python command.
SolutionRun python3 with the filename
Use:
python3 hello.py
python3 identifies the interpreter that was verified, and hello.py
identifies the source file to run. ls can confirm that a file exists, but
it does not execute Python code.
Exercise: Predict interactive-session output
What becomes visible after these commands are entered in one interactive session?
>>> total = 8
>>> total / 2
>>> print("total:", total)
Select one choice, then check.
HintEvaluate one command at a time
Assignment stores a value. A bare expression displays its value in an
interactive session. print() produces ordinary output.
SolutionTwo results become visible
total = 8 stores the value without displaying it. total / 2 evaluates to
4.0, which the interactive session displays. The final command prints:
total: 8
Read and Correct Errors
Exercise: Interpret output and a traceback
Read this result:
starting
Traceback (most recent call last):
File "report.py", line 2, in <module>
print(score)
NameError: name 'score' is not defined
Which interpretation uses all the relevant evidence?
Select one choice, then check.
HintRead the result in two parts
Text before Traceback may be ordinary output. In the traceback, locate the
source line and then read the final category and message.
SolutionThe second instruction failed
The program printed starting before the error occurred. Python then reached
line 2, where print(score) tried to use a name with no assigned value. The
final line classifies the problem as a NameError and identifies score.
Exercise: Correct a division error
Run the program, read the error report, and make the smallest change that
produces share: 5.0.
Correct a division error
Ready to run.
HintUse the category and target output
ZeroDivisionError identifies the failing operation. Which nonzero value for
people makes 20 / people equal 5?
SolutionDivide the total among four people
Change the second line to:
people = 4
The program then evaluates 20 / 4 and prints share: 5.0. The other two
lines do not need to change.
Inspect Comments and Intermediate Values
Exercise: Distinguish comments from visible text
Which two statements are correct?
Select every choice that applies, then check.
HintApply the comment rule at the hash symbol
Outside quoted text, # begins a comment that continues to the end of the
line. Code before that symbol still runs.
SolutionPosition and quotation marks determine the meaning
Python ignores the full line # print("hidden"). In
print("# visible"), the hash symbol is inside a string and is printed.
For value = 4 # start, the assignment runs and only the text after # is
ignored.
Exercise: Use intermediate output to find the wrong operation
The intended result is cost: 24. The labeled inputs are already correct. Run
the program and change only the calculation of cost.
Inspect the calculation
Ready to run.
HintTrust the verified inputs
The printed price is 6 and the printed quantity is 4. Which operation
produces the expected cost of 24?
SolutionMultiply price by quantity
Replace the calculation with:
cost = price * quantity
The intermediate output shows that the two inputs already have the intended
values. Only the operation that creates cost needs to change.
Complete a Small Experiment
Exercise: Revise one assumption and its comment
The program models distance as speed * time. Change only time so the output
contains distance: 15, and update the comment so it describes the new
experiment.
Run a controlled experiment
Ready to run.
HintWork backward, then keep the explanation accurate
Solve 5 * time = 15. After changing time, revise the number stated in the
comment so the comment and code agree.
SolutionUse three time units and update the comment
One complete revision is:
The change from 2 to 3 is the only change that affects execution. Updating
the comment keeps its explanation consistent with the program.
These exercises complete the chapter's first programming cycle. The next chapter examines names, values, and types in more detail, including what assignment means and why different kinds of values support different operations.