Python, Concept by Concept › Loops & Counting
Exercises: Loops & Counting
This bank is intentionally larger than one lesson. Core: Exercises 1–12 are the main path. More Practice: Exercises 13–29 offer extra repetitions and new genres. Challenges: Exercises 30–34 are optional stretch work; no core exercise depends on them.
For every exercise, use the fixed given values, follow the specification, and produce the exact output shown. Every loop must move toward its stopping condition. Both x = x + 1 and the shorter x += 1 are available.
Exercise 1: Training-Lap Counter
A robot completes a fixed number of training laps. The board needs only the final count, not one line per lap.
Specification: Given the positive integer lap_goal, start laps at 0. Use a while loop that adds 1 to laps until it reaches lap_goal. After the loop, print exactly Count: <laps>.
Worked sample — given values:
lap_goal = 6
Expected output:
Count: 6
Real program
The real program reads a lap goal with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 2: Repair the Last Number
A display should include both endpoints from 0 through 5, but its boundary stops one number too early.
Specification: Repair only the while condition so the program prints every integer from 0 through 5, one per line. Keep the counter update unchanged. Put the repaired program in an empty cell in your exercises notebook.
Worked sample — broken program:
i = 0
while i < 5:
print(i)
i = i + 1
Expected output after the repair:
0
1
2
3
4
5
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 3: Fundraiser Running Total
A class adds the whole-dollar donations from 1 dollar through a fixed final amount. A running total avoids writing every addition separately.
Specification: Given the positive integer last_amount, use a while loop and a running-total accumulator to add every integer from 1 through last_amount, inclusive. After the loop, print exactly Sum: <total>.
Worked sample — given values:
last_amount = 12
Expected output:
Sum: 78
Real program
The real program reads whole-dollar donations until a 0 line with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 4: Multiples-of-Four Count
A game awards a marker on turns divisible by 4. The referee needs to know how many markers appear in the first several turns.
Specification: Given the positive integer last_turn, use a while loop to check every integer from 1 through last_turn. Add 1 to count only when the turn is divisible by 4. After the loop, print exactly Count: <count>.
Worked sample — given values:
last_turn = 20
Expected output:
Count: 5
Real program
The real program reads a final turn with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 5: Factorial Machine
A puzzle machine multiplies every positive integer from 1 through a small input number.
Specification: Given an integer n from 1 through 10, use a while loop to calculate 1 * 2 * ... * n. Initialize product to 1, not 0. After the loop, print exactly Product: <product>.
Worked sample — given values:
n = 6
Expected output:
Product: 720
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 6: Digit-Sum Scanner
A badge scanner checks a positive code by adding its digits. The last digit is n % 10, and n // 10 removes that digit.
Specification: Given the positive integer n, use a while loop that repeats until n becomes 0. Peel and add one digit each pass. After the loop, print exactly Digit sum: <digit_total>.
Worked sample — given values:
n = 7314
Expected output:
Digit sum: 15
Real program
The real program reads a positive code with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 7: Computed Stop: Collatz Steps
A number follows this rule until it reaches 1: divide an even value by 2; otherwise multiply it by 3 and add 1.
Specification: Given the positive integer n = 7, set steps to 0. Use the plain sentinel condition while n != 1. Update n with the Collatz rule and add 1 to steps once per update. After the loop, print exactly Steps: <steps>.
Worked sample — given values:
n = 7
Expected output:
Steps: 16
Real program
The real program reads a positive starting number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 8: Shared-Tile Size
Two positive strip lengths need the largest tile size that divides both exactly. Repeated subtraction keeps the common divisors while shrinking the larger value.
Specification: Given positive integers a and b, use while a != b. When a is larger, replace it with a - b; otherwise replace b with b - a. When they match, print exactly GCD: <a>.
Worked sample — given values:
a = 84
b = 36
Expected output:
GCD: 12
Real program
The real program reads two positive strip lengths, one per line with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 9: Score-Tier Census
A twelve-level game groups each level as low, middle, or high. The highest matching threshold must win.
Specification: Check every integer score from 1 through 12 with a while loop. Use one if/elif/else ladder: scores at least 9 are high, scores at least 5 are middle, and the rest are low. Count each tier, then print exactly the three summary lines shown.
Worked sample — given values:
last_score = 12
Expected output:
Low: 4
Middle: 4
High: 4
Real program
The real program reads scores until a 0 line with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 10: Countdown Liftoff
A launch clock must show every positive count before liftoff.
Specification: Use a while loop to count from start down to 1, then print Liftoff!. Add a purpose comment explaining the countdown.
Worked sample — given values:
start = 5
Worked sample — expected output:
5
4
3
2
1
Liftoff!
Real program
The real program reads a starting number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 11: Count the Digits
A scanner needs the number of decimal digits in a positive code.
Specification: Use // 10 repeatedly in a while loop. Count each removed digit and print Digits: <count>. Add a purpose comment.
Worked sample — given values:
n = 90210
Worked sample — expected output:
Digits: 5
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 12: Reverse a Number
A display reads a positive number backward.
Specification: Peel digits with % 10 and // 10; build a reversed integer with an accumulator. Print Reversed: <value>. Add a purpose comment.
Worked sample — given values:
n = 3721
Worked sample — expected output:
Reversed: 1273
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 13: More Practice: Palindrome Number
A mirrored number reads the same from both ends.
Specification: Save the given value in original before peeling digits. Reverse the number with a loop, compare it with original, and print the matching palindrome sentence.
Worked sample — given values:
n = 12321
Worked sample — expected output:
12321 is a palindrome
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 14: More Practice: Doubling Past a Limit
A signal doubles until it passes its safety limit.
Specification: Use while True: and a reachable break. Start with value and count each doubling. Stop as soon as the new value exceeds limit; print its value and count.
Worked sample — given values:
value = 5
limit = 500
Worked sample — expected output:
640 after 7 doublings
Real program
The real program reads a starting value and a limit, one per line with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 15: More Practice: Sum of Even Digits
A badge scanner reports both how many even digits it saw and their sum.
Specification: Peel every digit with a while loop. Count and add only digits divisible by 2. Print the two exact summary lines.
Worked sample — given values:
n = 482615
Worked sample — expected output:
Even digits: 4
Even-digit sum: 20
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 16: More Practice: Count the Odd Digits
A second scanner counts odd digits.
Specification: Peel each digit and add 1 to a counter only when the digit is odd. Print Odd digits: <count>.
Worked sample — given values:
n = 73185
Worked sample — expected output:
Odd digits: 4
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 17: More Practice: Largest Digit
A scanner keeps its largest digit so far.
Specification: Peel digits one at a time. When a digit exceeds best, replace best. Print Largest digit: <best>.
Worked sample — given values:
n = 58193
Worked sample — expected output:
Largest digit: 9
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 18: More Practice: Loop-Built Star Bar
Build a bar one star at a time.
Specification: Use a while loop and bar = bar + "*" to build exactly n stars. Print the labeled bar; do not use string repetition for this task.
Worked sample — given values:
n = 7
Worked sample — expected output:
Stars: *******
Real program
The real program reads a bar length with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 19: More Practice: First Square Over 300
Find the first positive integer whose square exceeds a limit.
Specification: Start n at 1. Use while n <= 100: and break at the first square over limit. Print the number and its square. (The n <= 100 bound works for any limit below 10000.)
Worked sample — given values:
n = 1
limit = 300
Worked sample — expected output:
18 squared is 324
Real program
The real program reads a square limit with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 20: More Practice: Fix the Infinite Loop
The counter never changes, so this program never stops.
Specification: Read the broken code without running it. Add count = count + 1 inside the loop and make the repaired program print 0 through 4.
Broken program (read; do not run):
count = 0
while count < 5:
print(count)
Error or symptom: No exception is raised; the program keeps printing 0 forever.
Worked sample — expected output:
0
1
2
3
4
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 21: More Practice: Fix the Missing Starting Value
The total is used before it has a value.
Specification: Read the broken code and its NameError. Add total = 0 before the loop so the repaired program prints 10.
Broken program (read; do not run):
n = 1
while n <= 4:
total = total + n
n = n + 1
print(total)
Error or symptom: NameError: name 'total' is not defined
Worked sample — expected output:
10
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: a number
Exercise 22: More Practice: Fix the Missing Colon
Python needs a colon after the while condition.
Specification: Read the broken code and its SyntaxError. Add the colon so the repaired program prints 3, 2, 1.
Broken program (read; do not run):
n = 3
while n > 0
print(n)
n = n - 1
Error or symptom: SyntaxError: expected ':'
Worked sample — expected output:
3
2
1
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 23: More Practice: Fix the Unindented Body
Python needs an indented body after while.
Specification: Read the broken code and its IndentationError. Indent both body lines and add print(total) after the loop so the repaired program prints 6.
Broken program (read; do not run):
total = 0
n = 1
while n <= 3:
total = total + n
n = n + 1
Error or symptom: IndentationError: expected an indented block after 'while' statement
Worked sample — expected output:
6
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: a number
Exercise 24: More Practice: Lucky Sevens
Count every 7 digit in a positive number.
Specification: Peel the digits with a while loop and count a digit only when it equals 7. Print the labeled count.
Worked sample — given values:
n = 707172
Worked sample — expected output:
Sevens: 3
Real program
The real program reads a positive number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 25: More Practice: Savings Streak
A saver deposits 10 the first week and 5 more each following week.
Specification: Run a while loop for 8 weeks. Add that week's deposit to a total, then grow the next deposit by 5. Print the final week and total.
Worked sample — given values:
deposit = 10
weeks = 8
Worked sample — expected output:
Week 8 total: 220
Real program
The real program reads the number of weeks with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 26: More Practice: Triangular Numbers
Each term adds the next positive number to the running total.
Specification: Build terms for 1, 1+2, and so on while the next total stays below 50. Put the terms in one space-separated string, with no trailing space, then print it.
Worked sample — given values:
number = 1
limit = 50
Worked sample — expected output:
1 3 6 10 15 21 28 36 45
Real program
The real program reads a limit with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 27: More Practice: Decimal to Binary
Repeated division by 2 reveals binary digits from right to left.
Specification: While n is positive, prepend str(n % 2) to a text accumulator, then replace n with n // 2. Print the binary text.
Worked sample — given values:
n = 37
Worked sample — expected output:
100101
Real program
The real program reads a positive decimal number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: a number
Exercise 28: More Practice: Guessing Robot
The secret is 42 in the inclusive range 1 through 100. A robot always guesses the middle integer.
Specification: Start low = 1, high = 100. Use while True: and break when the middle guess matches secret. Otherwise narrow the interval. Build the exact guess transcript and print the final count. The real version reads the secret and uses the same robot.
Worked sample — given values:
secret = 42
Worked sample — expected output:
Guesses: 50 25 37 43 40 41 42
Found 42 in 7 guesses
Real program
The real program reads the secret number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 29: More Practice: Countdown Bars
A display shows a shorter bar at each countdown step.
Specification: Use a while loop from start down to 1. Build each row with the number, one space, and "#" * n; print four rows with no trailing spaces.
Worked sample — given values:
start = 4
Worked sample — expected output:
4 ####
3 ###
2 ##
1 #
Real program
The real program reads a starting number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 30: Remainder GCD
A faster greatest-common-divisor method replaces the pair (a, b) with (b, a % b) until the second value is 0.
Specification: Given positive integers a and b, use while b != 0. Inside the loop, save a % b in remainder, then move the old b into a and remainder into b. After the loop, print exactly GCD: <a>.
Worked sample — given values:
a = 252
b = 105
Expected output:
GCD: 21
Real program
The real program reads two positive strip lengths, one per line, with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 31: Probe Mission 19
A new probe begins at 19 and follows the Collatz rule until it reaches 1. This time the stop test must exit from the middle.
Specification: Given n = 19, set steps to 0.
Use while True:.
First break when n == 1; otherwise apply the Collatz rule and add 1 to steps.
After the loop, print exactly Steps: <steps>.
The given start value is guaranteed to reach 1.
Worked sample — given values:
n = 19
Expected output:
Steps: 20
Real program
The real program reads a positive starting number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 32: Collatz Peak
Track the highest value a Collatz journey reaches.
Specification: Start peak at the given number. Apply the Collatz update until 1; replace peak whenever the new value is larger. Print its peak.
Worked sample — given values:
n = 15
Worked sample — expected output:
Peak: 160
Real program
The real program reads a positive starting number with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: one line
Exercise 33: Powers of Two
Show the first eight powers of two and their sum.
Specification: Use a while loop to print 2^0 = 1 through 2^7 = 128, then print Total: 255. Build powers by doubling and add each to a total.
Worked sample — given values:
power = 1
exponent = 0
Worked sample — expected output:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
Total: 255
Real program
The real program reads the number of powers to show with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Exercise 34: Count the Steps
Compare two ways to calculate the GCD of 270 and 192.
Specification: Count one step per subtraction in the first algorithm and one step per remainder update in the second. Print both step counts and the shared GCD. Use separate named variables for the two algorithms; do not use tuple assignment.
Worked sample — given values:
a = 270
b = 192
Worked sample — expected output:
Subtraction steps: 10
Remainder steps: 4
GCD: 6
Real program
The real program reads two positive numbers, one per line with input().
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines