Getting comfortable with loops is a crucial part of starting out in Python. Working through practical problems, rather than just memorizing syntax, helps new programmers grasp how loops function and why they show up so often in everyday code.
Dividing Tiles with Loops
Picture a school setting up a large square floor for an event. The floor has a row of N tiles, and the teacher wants to split them into equal groups so that every tile is used. This setup gives a classic way to practice finding divisors with a while loop in Python. For 12 tiles, you can arrange them as 1 group of 12, 2 groups of 6, 3 groups of 4, 4 groups of 3, 6 groups of 2, or 12 groups of 1. The code below prints all divisors of 12:
no = 12
divisor = 1
while divisor <= 12:
if no % divisor == 0:
print(divisor)
divisor = divisor + 1
This method lets beginners see how loops handle repeated checks and spot number patterns. For N=12, the divisors are 1, 2, 3, 4, 6, and 12, so the loop prints six results. The algorithm checks every integer from 1 to 12 and runs one remainder operation for each, totaling 12 divisor checks. Its time complexity is O(N), and it uses O(1) memory, not counting output. For bigger N, a faster method would stop at the square root of N and print paired divisors, but this one-to-one approach is especially clear for those just learning how loops work.
Identifying Special Numbers
Certain numbers can only be split into groups in two ways: all together or one at a time. Here, these are called Special Numbers. In math, the standard term is prime number: a positive integer greater than 1 with exactly two distinct positive divisors, 1 and itself. For example, 7 can only be grouped as 1 group of 7 or 7 groups of 1, but 8 can also be split as 2 groups of 4. The code below checks if a number is Special by counting its divisors:
N = int(input("Enter a number: "))
divisor = 1
count = 0
while divisor <= N:
if N % divisor == 0:
count = count + 1
divisor = divisor + 1
if count == 2:
print("Special Number")
else:
print("Not a Special Number")
This puzzle brings in the idea of prime numbers through a hands-on coding task, helping learners connect math concepts with programming logic. The code uses N % divisor == 0 to check for divisors, which is the core test for this purpose. The counter goes up for each successful check, and the final count == 2 condition correctly spots primes when the input is above 1. Inputs 0 and 1 need special handling since neither is prime, and negative numbers fall outside the usual beginner scope. A solid version should check that N > 1 before using the divisor-count rule.
Finding VIP Numbers with Loops
In a security system, VIP lockers get numbers that can only be divided by 1 and themselves. The task is to find the first five such numbers. This exercise uses nested loops to check each number for its divisors and prints it if it fits:
num = 2
count = 0
while count < 5:
divisor = 1
factors = 0
while divisor <= num:
if num % divisor == 0:
factors = factors + 1
divisor = divisor + 1
if factors == 2:
print(num) # 2 3 5 7 11
count = count + 1
num = num + 1
This example shows how loops can work together to solve more involved problems, like making a list of prime numbers. The method is simple but can be slow for large ranges, since it repeats the divisor search for each candidate. For faster prime number generation, algorithms such as the Sieve of Eratosthenes are widely taught in computer science and programming classes. For more details, see the official Python documentation on loops.
Checking Special Cards from 1 to 20
Imagine a teacher writes numbers 1 through 20 on cards and wants to show only those that are Special Numbers. The code below checks each number in the range and prints it if it qualifies:
num = 1
while num <= 20:
divisor = 1
count = 0
while divisor <= num:
if num % divisor == 0:
count = count + 1
divisor = divisor + 1
if count == 2:
print(num) # 2 3 5 7 11 13 17 19
num = num + 1
Working through these puzzles helps beginners gain confidence with loops and see how to use them for real tasks. The math examples also clarify the difference between factors and groups: a divisor divides another integer evenly, and the quotient gives the matching group size. For 12, divisor pairs are (1,12), (2,6), and (3,4); counting both numbers in each pair gives six positive divisors. A perfect square has one unpaired middle divisor, since both members of one pair are the same. This helps prevent confusion between the number of factor pairs and the total number of divisors. For more on Python basics, the earlier breakdown of list comprehensions gives another hands-on way to write clear, concise code.
