Home of real teaching & learning
  • Full support for teachers
  • Focus on critical thinking
  • Engaging classroom activities
  • Integrated student eBook
  • Assessed tasks / qBank
  • Practice exam questions

The InThinking Guarantee: Our sites are written by expert practitioners and not by AI

See our AI policy

Disclaimer: InThinking subject sites are neither endorsed by nor connected with the International Baccalaureate Organisation.

Don't miss out, find out!

B2.1 Broken Code challenges - Python

B2.1.1: Data Types & Variables

# Topic B2.1.1: Variables & Data Types
# CHALLENGE: We want to print a birthday message.
# But this code crashes with a "TypeError".
# Fix the code to make the data types compatible.

name = "Sam"
age = 17

# BUG IS HERE
message = "Happy Birthday " + name + ", you are now " + age

print(message)

B2.1.2: String Manipulation (Substrings)

# Topic B2.1.2: String Manipulation
# CHALLENGE: We want to extract the DOMAIN from an email.
# Input: "[email protected]"
# Target Output: "school.com"
# Actual Output: "@school.com" (It includes the @ symbol!)
# Why is the slice capturing the separator?

email = "[email protected]"

# Find the position of the "@" symbol (Index 7)
at_symbol_index = email.find("@")

domain = email[at_symbol_index:] 

print("Domain:", domain)

B2.1.3: Exception Handling

# Topic B2.1.3: Exception Handling
# CHALLENGE: This calculator crashes if the user enters 0 as the second number.
# (ZeroDivisionError).
# Wrap the dangerous code in a try/except block to handle the error nicely.

print("Division Calculator")
num1 = int(input("Enter top number: ")) # e.g. 10
num2 = int(input("Enter bottom number: ")) # e.g. 0

# BUG: This line is unprotected!
result = num1 / num2

print("Result:", result)

B2.1.4: Debugging (Trace Tables)

# Topic B2.1.4: Debugging & Tracing
# CHALLENGE: This code calculates the average of a list of grades.
# But the result is always wrong!


grades = [80, 90, 70, 100]
count = len(grades)

for grade in grades:

    total_score = 0
    total_score = total_score + grade
    print(f"Current Grade: {grade}, Running Total: {total_score}")

average = total_score / count
print("Final Average:", average)
All materials on this website are for the exclusive use of teachers and students at subscribing schools for the period of their subscription. Any unauthorised copying or posting of materials on other websites is an infringement of our copyright and could result in your account being blocked and legal action being taken against you.

Help