WEB PRACTICE CHAPTER 02

Decisions and Conditions

Ten additional coding tasks · W011–W020

These extra website exercises are not a substitute for the book's exact numbered questions. Follow your printed book separately using the 100-challenge tracker.
Chapter 2 code

Choose starters to try yourself, or download a separate worked example pack after attempting the tasks.

Chapter 2 starters ↓Chapter 2 worked examples ↓
WEB PRACTICE W011

Age eligibility

Determine whether someone aged 14 is eligible for a 13+ coding workshop.

Reveal a hint

Use >= for an inclusive lower bound.

Reveal example code (after trying it yourself)
age = 14
if age >= 13:
    print("Eligible")
else:
    print("Not eligible")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W012

Even or odd

Decide whether the number 37 is even or odd.

Reveal a hint

An even integer has a remainder of zero on division by 2.

Reveal example code (after trying it yourself)
number = 37
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W013

Temperature bands

Classify a temperature of 17°C as cold, mild or hot.

Reveal a hint

Put the lowest threshold in the first branch.

Reveal example code (after trying it yourself)
temp = 17
if temp < 10:
    print("Cold")
elif temp < 25:
    print("Mild")
else:
    print("Hot")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W014

Pass or retry

Display Pass for a quiz score of 68 when the pass mark is 50.

Reveal a hint

Use >= so exactly 50 also passes.

Reveal example code (after trying it yourself)
score = 68
print("Pass" if score >= 50 else "Try again")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W015

Discount threshold

Apply a 10% discount to a £60 order when spending reaches £50.

Reveal a hint

Calculate the adjusted price in only one branch.

Reveal example code (after trying it yourself)
total = 60.0
if total >= 50:
    total *= 0.90
print(f"Pay £{total:.2f}")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W016

Username length

Check whether a username is at least six characters long.

Reveal a hint

Use len(username).

Reveal example code (after trying it yourself)
username = "coder72"
if len(username) >= 6:
    print("Length accepted")
else:
    print("Too short")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W017

Safety equipment

A workshop requires a helmet and safety glasses. Check both conditions.

Reveal a hint

The and operator requires both values to be True.

Reveal example code (after trying it yourself)
helmet = True
glasses = False
if helmet and glasses:
    print("Ready")
else:
    print("Check equipment")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W018

Ticket age band

Choose a child, teen or adult ticket for someone aged 16.

Reveal a hint

Make the conditions mutually exclusive.

Reveal example code (after trying it yourself)
age = 16
if age < 12:
    ticket = "child"
elif age < 18:
    ticket = "teen"
else:
    ticket = "adult"
print(ticket)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W019

Leap year rules

Check whether 2024 is a leap year using the full divisible-by-4/100/400 rule.

Reveal a hint

Century years must be divisible by 400.

Reveal example code (after trying it yourself)
year = 2024
leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
print("Leap year" if leap else "Not leap year")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W020

Safe range

Decide whether a sensor reading of 27 is inside the inclusive safe range 18–30.

Reveal a hint

Use a chained comparison.

Reveal example code (after trying it yourself)
reading = 27
if 18 <= reading <= 30:
    print("Safe range")
else:
    print("Outside range")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.