Coding Hub  /  Practice

week 1–4 problems

Twelve problems on variables, conditions and loops. Write each one yourself before you open the answer — reading a solution teaches you almost nothing.

How to work through these

Twelve problems, roughly in order of difficulty. Each has a starter file, the output expected, and a collapsible answer.

00 Before you start

Make one file per problem, named problem_01.py and so on, and run each with python problem_01.py.

There is more than one correct answer to every problem here. If yours produces the right output and you can explain every line, it is right — even if it looks nothing like mine.

Open the answer only after you have something that runs, even if it runs wrongly. The struggle is the part that teaches.

what each problem uses
ProblemsTopic
1–3Variables, input, type conversion
4–6Conditions
7–9Loops
10–12Loops with lists, and putting it together

Variables and input

Problems 1 to 3.

01 Greeting

Ask for a name, then print a greeting using it. Use an f-string, not +.

problem_01.py
name = input("What is your name? ")

# your code here
expected output
What is your name? Ada
Hello, Ada! Nice to meet you.
Show one working answer
name = input("What is your name? ")
print(f"Hello, {name}! Nice to meet you.")

02 Rectangle area

Ask for a width and a height, then print the area and the perimeter.

Stuck? input() gives you a string. You will need int().

problem_02.py
width = input("Width? ")
height = input("Height? ")

# your code here
expected output
Width? 4
Height? 6
Area: 24
Perimeter: 20
Show one working answer
width = int(input("Width? "))
height = int(input("Height? "))

print(f"Area: {width * height}")
print(f"Perimeter: {2 * (width + height)}")

03 Seconds to minutes

Ask for a number of seconds and print it as whole minutes and leftover seconds. 150 seconds is 2 minutes and 30 seconds.

Stuck? // gives whole minutes, % gives the remainder.

problem_03.py
total = int(input("Seconds? "))

# your code here
expected output
Seconds? 150
2 minutes and 30 seconds
Show one working answer
total = int(input("Seconds? "))

minutes = total // 60
seconds = total % 60
print(f"{minutes} minutes and {seconds} seconds")

Conditions

Problems 4 to 6.

04 Pass or fail

Ask for a score out of 100 and print a grade: 70 or more is a pass, 50 to 69 is a near miss, under 50 is a fail.

Stuck? Use elif. Order matters — check the highest band first.

problem_04.py
score = int(input("Score? "))

# your code here
expected output
Score? 64
Near miss
Show one working answer
score = int(input("Score? "))

if score >= 70:
    print("Pass")
elif score >= 50:
    print("Near miss")
else:
    print("Fail")

05 Odd or even

Ask for a whole number and say whether it is odd or even. Then handle zero as its own case, printing Zero.

Stuck? n % 2 == 0 is the even test. Check for zero before that.

problem_05.py
n = int(input("A number? "))

# your code here
expected output
A number? 7
Odd
Show one working answer
n = int(input("A number? "))

if n == 0:
    print("Zero")
elif n % 2 == 0:
    print("Even")
else:
    print("Odd")

06 Leap year

A year is a leap year if it divides by 4, except years dividing by 100, unless they also divide by 400. So 2024 yes, 1900 no, 2000 yes.

Stuck? You can do it with one condition using and and or, or with nested ifs. Both are fine.

problem_06.py
year = int(input("Year? "))

# your code here
expected output
Year? 1900
1900 is not a leap year
Show one working answer
year = int(input("Year? "))

is_leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0

if is_leap:
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")

Loops

Problems 7 to 9.

07 Countdown

Print the numbers from 10 down to 1, one per line, then Liftoff!.

Stuck? range takes a third argument, the step. It can be negative.

problem_07.py
# your code here
expected output
10
9
8
...
1
Liftoff!
Show one working answer
for n in range(10, 0, -1):
    print(n)

print("Liftoff!")

08 Times table

Ask for a number and print its times table from 1 to 12, as 3 x 1 = 3 on each line.

problem_08.py
n = int(input("Which table? "))

# your code here
expected output
Which table? 3
3 x 1 = 3
3 x 2 = 6
...
3 x 12 = 36
Show one working answer
n = int(input("Which table? "))

for i in range(1, 13):
    print(f"{n} x {i} = {n * i}")

09 FizzBuzz

Print the numbers 1 to 30. For multiples of 3 print Fizz instead, for multiples of 5 print Buzz, and for multiples of both print FizzBuzz.

Stuck? Check for both first. If you check 3 first, 15 never reaches the FizzBuzz case.

problem_09.py
for n in range(1, 31):
    # your code here
    pass
expected output
1
2
Fizz
4
Buzz
Fizz
...
14
FizzBuzz
Show one working answer
for n in range(1, 31):
    if n % 3 == 0 and n % 5 == 0:
        print("FizzBuzz")
    elif n % 3 == 0:
        print("Fizz")
    elif n % 5 == 0:
        print("Buzz")
    else:
        print(n)

Loops with lists

Problems 10 to 12.

10 Average

Given the list of scores below, print the lowest, the highest and the average. Show the average to one decimal place.

Stuck? min(), max(), sum() and len() all take a list. For the formatting, f"{x:.1f}".

problem_10.py
scores = [72, 55, 91, 68, 84, 47]

# your code here
expected output
Lowest: 47
Highest: 91
Average: 69.5
Show one working answer
scores = [72, 55, 91, 68, 84, 47]

print(f"Lowest: {min(scores)}")
print(f"Highest: {max(scores)}")
print(f"Average: {sum(scores) / len(scores):.1f}")

11 Count the long words

Given the list of plants below, count how many have names longer than five characters, and print those names.

Stuck? Start a counter at 0 before the loop, not inside it.

problem_11.py
plants = ["fern", "monstera", "ivy", "philodendron", "cactus"]

# your code here
expected output
monstera
philodendron
cactus
3 plants have long names
Show one working answer
plants = ["fern", "monstera", "ivy", "philodendron", "cactus"]

count = 0
for plant in plants:
    if len(plant) > 5:
        print(plant)
        count += 1

print(f"{count} plants have long names")

12 Shopping total

Keep asking for a price until the user types done. Then print how many items were entered and the total. Typing done straight away should print a total of 0, not crash.

Stuck? A while True loop with a break is the clearest shape. Check for "done" before converting to a number.

problem_12.py
total = 0
count = 0

# your code here
expected output
Price (or done)? 4.50
Price (or done)? 2.25
Price (or done)? done
2 items, total $6.75
Show one working answer
total = 0
count = 0

while True:
    answer = input("Price (or done)? ").strip().lower()
    if answer == "done":
        break
    total += float(answer)
    count += 1

print(f"{count} items, total ${total:.2f}")