AI for Youth Academy Future Scholars Research Initiative

Year 1 · Week 14

Chapter 14: Teams, Lists, Loops, and Your Own Class

Last week you gave your objects methods — attack, heal, show_stats. This week you will put those objects into lists, loop over teams, and run a full battle. Then in Session 5, you will design your very own class from scratch.

Session 4: Teams, Lists, and Loops

Duration: 60 minutes

Learning Goals

  • Store multiple objects in a list
  • Loop over a list of objects and call methods on each
  • Debug common errors: missing self, missing (), wrong variable name

Building a Team (15 min)

Once you have a class, you can make as many objects as you want and store them in a list — just like any other value:

team = [
    Pokemon('Pikachu',    100, 20),
    Pokemon('Charmander',  90, 18),
    Pokemon('Squirtle',    95, 15),
]

# Loop over the team and print stats
for p in team:
    print(p)          # uses __str__

# Find the Pokémon with the most HP
strongest = team[0]
for p in team:
    if p.hp > strongest.hp:
        strongest = p
print(f'Strongest: {strongest.name}')

Simple Battle Simulation (25 min)

Now we build a simple two-team battle. Copy this step by step:

import random

team_a = [Pokemon('Pikachu', 100, 20), Pokemon('Eevee', 80, 15)]
team_b = [Pokemon('Geodude', 70, 25), Pokemon('Onix', 90, 22)]

def alive(team):
    return [p for p in team if p.hp > 0]

round_num = 1
while alive(team_a) and alive(team_b):
    print(f'--- Round {round_num} ---')
    attacker = random.choice(alive(team_a))
    defender = random.choice(alive(team_b))
    attacker.attack(defender)
    if alive(team_b):   # team_b counter-attacks
        attacker2 = random.choice(alive(team_b))
        defender2 = random.choice(alive(team_a))
        attacker2.attack(defender2)
    round_num += 1

if alive(team_a):
    print('Team A wins!')
else:
    print('Team B wins!')

Debugging Clinic (15 min)

Your teacher has a broken version of the battle code with three bugs. Race your partner to find and fix all three. Bugs are hidden in:

  1. A method definition is missing self in the right place
  2. A method is called without ()
  3. The loop variable name is used inconsistently

Exit Ticket

  1. What does for p in team: p.show_stats() do?
  2. Why do we write alive(team_a) as a function instead of checking HP inside the while condition directly?

Session 5: Design Your Own Class

Duration: 60 minutes

Learning Goals

  • Choose a theme and design a class on paper before writing code
  • Independently implement a class with __init__, 4 methods, and __str__
  • Explain your design to a partner

Design First — No Code Yet! (15 min)

Code It (35 min)

Turn your object card into a working Python class. Requirements:

  1. __init__ that sets all 4 attributes
  2. 4 methods — at least 2 must use if
  3. __str__ that returns a nice one-line summary
  4. At least one method must take a parameter (something passed in)
  5. Create 2 objects from your class and call at least 3 methods

Starter Code

Copy the skeleton below and replace the ____ with your own content:

class ________:   # ← pick your theme name
    def __init__(self, name, _____, _____, _____):
        self.name = name
        # TODO: set your other 3 attributes

    def method_1(self):
        # TODO: write a simple action
        pass

    def method_2(self):   # ← must use if
        # TODO: write a method with if/else
        pass

    def method_3(self, target):   # ← takes a parameter
        # TODO: write a method that uses target
        pass

    def method_4(self):   # ← must use if
        # TODO: write another method with if/else
        pass

    def __str__(self):
        return f'...'   # TODO: return a nice one-line summary

Step-by-Step Guide

  1. Step 1: Pick a theme name. Replace class ________: with your own class name (e.g. Dragon).
  2. Step 2: Fill in the parameters and attributes of __init__. Think about what information your object needs to remember.
  3. Step 3: Write method_1 — a simple action, like roar() or celebrate().
  4. Step 4: Write method_2 — it must contain an if. For example, "if HP is low, recover some."
  5. Step 5: Write method_3 — it receives a target parameter. For example, attack another object.
  6. Step 6: Write method_4 — it also must contain an if. For example, "if age is high enough, increase power."
  7. Step 7: Write __str__ using an f-string to return a one-line summary.
  8. Step 8: Create 2 objects and call at least 3 methods to test.
Show Full Example (Dragon class)
class Dragon:
    def __init__(self, name, fire_power, hp, age):
        self.name       = name
        self.fire_power = fire_power
        self.hp         = hp
        self.age        = age

    def breathe_fire(self, target):
        target.hp -= self.fire_power
        if target.hp <= 0:
            print(target.name, 'is defeated!')

    def rest(self):
        if self.hp < 100:
            self.hp += 20
            print(f'{self.name} rests and recovers HP.')
        else:
            print(f'{self.name} is already at full HP!')

    def grow_up(self):
        self.age += 1
        if self.age >= 10:
            self.fire_power += 5
            print(f'{self.name} grew up! Fire power increased.')

    def roar(self):
        print(f'{self.name} ROAAAARS!')

    def __str__(self):
        return f'{self.name} (Age: {self.age}, HP: {self.hp}, Fire: {self.fire_power})'

# Create 2 objects and test
draco = Dragon('Draco', 30, 80, 9)
smoky = Dragon('Smoky', 25, 90, 11)

print(draco)
draco.breathe_fire(smoky)
print(smoky)
draco.rest()
draco.grow_up()

Show & Tell (10 min)

Each student opens Thonny and demos one method in the Python shell. The class asks: "What does that method do? What happens if it runs twice?"

Exit Ticket

Write one thing that was easy today and one thing that was confusing.

Key Vocabulary