Exercises

Predict branches and exact boundaries, trace both loop forms, build a summary, repair a stopping error, and account for unavailable or unvisited readings.

Decide and Trace

Q1. Predict a comparison and branch

What does this program display?

Choose one

Select one choice, then check.

HintCheck equality at the boundary

> does not include equality.

SolutionThe else branch runs

22 > 22 is false, so the program displays not high.

Not attempted
Review

Not marked done.

Q2. Test exact interval boundaries

Which condition treats 18 as ordinary and 22 as high?

Choose one

Select one choice, then check.

HintTest both boundary values

The condition must be true at 18 and false at 22.

SolutionUse a half-open interval

18 <= reading < 22 includes 18 but excludes 22, matching the chapter's ordinary and high branches.

Not attempted
Review

Not marked done.

Q3. Trace a for loop

What is the final value of count?

Compute it first, then check your number.

HintMake three trace rows

Record whether 18, 21, and 20 are each greater than or equal to 20.

SolutionTwo readings are counted

The condition is false for 18 and true for 21 and 20, so the final count is 2.

Not attempted
Review

Not marked done.

Summarize and Repair

Q4. Complete total and count

Complete the loop so the program displays total: 82, count: 4, and mean: 20.5.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintCarry state across iterations

Use total += reading and count += 1 inside the loop.

SolutionUpdate both running values
Not attempted
Review

Not marked done.

Q5. Repair an infinite loop

Repair the program so it displays 0, 1, and 2, then stops.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintChange step inside the loop

Add step = step + 1 after the print instruction.

SolutionAdvance once per iteration
Not attempted
Review

Not marked done.

Q6. Repair an off-by-one boundary

The program should display 1, 2, and 3. Which condition repairs it?

Choose one

Select one choice, then check.

HintInclude the last value

The body must run when step is exactly 3.

SolutionUse less than or equal to

step <= 3 is true for 1, 2, and 3, then false after the update to 4.

Not attempted
Review

Not marked done.

Account for Missing and Unvisited Data

Q7. Summarize available readings

Use continue to skip None, then complete the loop so the program displays mean: 20.5.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintHandle absence first

Use if reading is None: continue, then update total and count below that condition.

SolutionSkip before numerical work
Not attempted
Review

Not marked done.

Q8. Choose break, continue, or a condition

A marker means that all later values are invalid and must remain unvisited. Which control states that policy?

Choose one

Select one choice, then check.

HintDistinguish skip from stop

continue starts the next iteration; the required policy must prevent it.

SolutionBreak at the marker

break ends the nearest loop immediately. Values after the marker are unvisited and should be reported that way.

Not attempted
Review

Not marked done.

Pause and reflect

Which exercises were difficult, what mistake pattern did you notice, and what should you practice again? The note stays with this exercise set.

0 of 8 exercises marked done

Review

Not marked done.