Loops: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Print the numbers 1 to 10, each on its own line, using a for loop and range().
Print the numbers 5 to 15 using range() with a start and a stop.
Print the numbers 0 to 4 using range() with only one argument.
Print the numbers 10 down to 1 using a negative step.
Print all the even numbers from 2 to 20 using a step of 2.
Print each character of "Python" on its own line.
Print the 5 times table from 5 x 1 to 5 x 10, one line per row.
Print the numbers 1 to 5 using a while loop.
Count down from 5 to 1 with a while loop, then print "Liftoff!" after the loop finishes.
Starting from total = 100, keep halving it with a while loop while it stays above 1, printing each value to 2 decimal places.
Add up the numbers from 1 to 100 using a for loop and an accumulator variable, then print the total.
Multiply the numbers 1 to 5 together using an accumulator, then print the result. Note why this accumulator must start at 1.
Count how many characters are in "programming" using a loop and a counter, without using len().
Using nested for loops, print every pair (i, j) where both run from 1 to 3. There should be 9 lines.
Using nested loops, count how many times the inner body runs when the outer loop goes 1 to 4 and the inner goes 1 to 5. Print the final count.
Loop through the numbers 1 to 10, printing each, but stop completely as soon as you reach 5.
Loop through the numbers 1 to 10 and print every one except the multiples of 3.
Loop through the numbers 1 to 5. For the number 3 do nothing at all using pass, and print every other number.
Print the numbers 1 to 6 twice: once using continue to skip 3, and once using break to stop at 3, so the difference is obvious.
Loop through the numbers 1 to 5 printing each, and add an else block that prints "Finished normally". Note that it runs because no break happened.
Repeat the previous loop but break at 3. Show that the else block is now skipped.
Given word = "rhythm", search its characters for a vowel. Print "Has a vowel" and break if you find one, and use for/else to print "No vowels found" otherwise.
Count up from 1 while the value stays below 5, printing each, and add a while/else block that prints "Loop completed normally".
Loop through the numbers 1 to 10 and print "<n> is even" or "<n> is odd" for each.
Given word = "Python", print each character together with its index, by looping over range(len(word)).
Add up only the odd numbers from 1 to 50 and print the total.
Ask the user for a sentence and count how many vowels it contains.
Ask for a sentence and count how many characters are digits, using a loop and .isdigit().
Build a single string containing the squares of 1 to 5 separated by spaces, using +=, then print it tidied up.
Print the full multiplication table from 1x1 to 5x5 as a neat grid, one row per outer pass.
Print each character of "Python" with a position number starting at 1, by keeping your own counter variable.
Given first = "abc" and second = "xyz", print their characters side by side as a - x, b - y, c - z.
Ask the user for 5 numbers one at a time and print the largest, without using max().
Ask for 5 numbers and print the smallest, without using min().
Ask the user to enter 5 numbers one at a time, printing the running average after each entry.
Keep asking the user for numbers, adding each to a running total, until they type "done". Print the final total.
Use an infinite while True loop to keep asking for a number until the user enters a positive one, then break and print it.
Using nested loops over i and j from 1 to 9, find and print the first pair whose product is 24, then stop the inner loop. Observe that the outer loop keeps going.
Fix the previous question so that both loops stop at the very first match, using a flag variable.
Ask for a number and reverse its digits using a while loop with % and //.
Ask for a number and print the sum of its digits.
Ask for a number and count how many digits it has, using a loop rather than len(str(n)).
Ask for a number and compute its factorial with a for loop.
Ask for a base and an exponent and compute the power using repeated multiplication, without **.
Ask for two numbers and multiply them using repeated addition, without *.
Ask for two numbers and find their GCD using the Euclidean algorithm.
Ask for two numbers and find their LCM, using the GCD you just computed.
Ask for n and print the first n numbers of the Fibonacci sequence.
Ask for a number and check whether it is prime, using a for/else loop.
Ask for a word and print it reversed using a loop, without using slicing.
Ask for n and print every prime number from 2 up to n, using nested loops with break/else.
Ask for a number and check whether it is a perfect number โ one whose proper divisors add up to itself, like 6 = 1+2+3.
Ask for a 3-digit number and check whether it is an Armstrong number โ the cubes of its digits adding up to the number itself.
Ask for a number and check whether it is a palindrome, using only arithmetic โ no strings or slicing.
Ask for a sentence and count how many words it contains without using .split(). Walk it character by character and count each move from a space into a non-space, so repeated spaces don't inflate the count.
Ask for a sentence and count how many characters are uppercase and how many are lowercase.
Ask for a number and print the sum of only its even digits.
Ask for a number and print its largest digit.
Ask for a number and print its Collatz sequence: halve it when even, otherwise triple it and add 1, until it reaches 1. Print how many steps it took.
Ask for a number and a limit, then print that number's times table up to the limit.
Ask for a number and print its full multiplication table from 1 to 10, with the operands and results lined up in neat columns.
Ask for a principal, a rate and a number of years, then print the simple interest earned at the end of each year.
Ask for a principal, a rate and a number of years, then print the compound balance at the end of each year.
Ask for a savings goal, a starting balance and a monthly deposit. Loop month by month until the goal is reached, then print how many months it took.
Ask how many students there are, then loop that many times collecting present/absent, and print a final tally of both.
Ask how many students sat an exam, collect each mark, and print how many passed (40 or more) and how many failed.
Ask for a number of days, collect a temperature for each, and print the highest, the lowest and the average.
Ask for 6 numbers, using continue to skip any negative value, and print the average of only the numbers you actually accepted.
Ask for a threshold and then 5 numbers, and print how many of them were above the threshold.
Ask for 4 words one at a time and print the longest one, without storing them all.
Show an infinite-loop bug: write a while loop that never updates its condition variable, explain why it hangs, then give the fixed version.
Print range(1, 5) and range(1, 6) as loops and explain why printing 1 to 5 needs range(1, 6).
Show that reassigning the loop variable inside a for loop does not change how many times the loop runs.
Loop through the numbers 1 to 10 using all three: continue to skip evens, pass as a placeholder for 7, and break on reaching 9. Predict the output first.
Given password = "SecurePass", use a for/else loop to report whether it contains a digit โ without a separate flag variable.
Count and print exactly how many times the innermost line runs in a triple-nested loop of 3, 4 and 5, and state the general rule.
Add 0.1 to a total ten times in a loop and compare the result with 1.0. Explain the outcome and show the safe way to test it.
Show three range() calls that produce no iterations at all, and explain each one.
Print the numbers 10 down to 1 correctly, then show the two common broken attempts and explain what each gets wrong.
Ask how many numbers the user wants to enter, then find the second largest in a single pass, with no sorting.
Ask for n and count how many primes there are up to n, checking divisors only up to the square root of each candidate. Explain why that is enough.
Ask for a number and a digit, and count how many times that digit appears in the number, using arithmetic only.
Ask for a positive number and convert it to binary using a loop, without using bin() or a format specifier.
This should print the sum of 1 to 5 but prints something far too small. Find and fix the bug.
for i in range(1, 6):
total = 0
total += i
print(total)This nested loop should print a 3x3 grid of coordinates but prints the same row three times. Find and fix the bug.
for i in range(3):
for j in range(3):
print(f"({i}, {i})", end=" ")
print()Build a Number Guessing Game. The secret is 42. Keep asking until the player gets it, saying "Too high" or "Too low" each time, and report how many attempts it took.
Extend the guessing game with a limit of 5 attempts. Use for/else so the "you lost" message appears only when the player never guessed correctly.
Build a Menu-Driven Calculator that keeps running until the user chooses to exit, handling division by zero safely.
Build an ATM Loop. Start with a balance of 1000 and keep offering check, deposit, withdraw and exit until the user leaves, keeping the balance correct across iterations.
Build a Password Retry System. Allow 3 attempts at the password "python123", then lock out. Use for/else for the lockout message.
Build a Shopping Cart Loop. Keep asking for an item name and price until the user types done, then print how many items were bought, the total, and the average price.
Build a Multi-Question Quiz. Ask 3 questions in a loop-friendly way, track the score, and print a percentage and grade at the end.
Build a Times Table Grid. Ask for a size n and print a full n-by-n multiplication grid with a header row and column, all neatly aligned.
Build a Star Rating Collector. Ask how many reviews there are, collect a 1โ5 rating for each (rejecting anything out of range without counting it), then print the average and a star bar.
Build a Digit Analyser. Ask for a number and report, using loops only: how many digits it has, their sum, their product, the largest, the smallest, and how many are even.
Explain when you should reach for a while loop and when a for loop is the right tool, demonstrating one clear case of each.
Loop over the characters of "5a3!7z9". Skip letters with continue, use pass as a reserved placeholder for punctuation, and break at the first digit above 8. Predict the output first.
Show the same search written two ways โ once with a flag variable, once with for/else โ and say which you would submit in a code review.
Search "programming" for the letter "g" twice: once checking every character regardless, and once stopping at the first match. Count the comparisons each version makes and explain the difference.
Build a Loop Mastery Report โ the fullest demonstration of this topic. Using a sentinel-controlled while loop, collect numbers until the user types done. Without sum(), min() or max(), compute and print the count, total, average, minimum, maximum, the even and odd counts, and how many were above the average.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session