Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Building a Composed Warrior | Composition
Object-Oriented Programming in Python
Section 3. Chapter 4
single

single

bookChallenge: Building a Composed Warrior

Swipe to show menu

Task

Swipe to start coding

Practice using composition by building a simple warrior game:

  • Create a Weapon class with a name and attack value.

  • Create a Warrior class with a name that can equip a weapon (store it as an attribute).

  • The warrior should start without a weapon (None).

  • Add a method equip_weapon() to assign a weapon to the warrior.

  • Add a method show_weapon() that returns a string describing the current weapon:

    • If unarmed → <Name> is unarmed";
    • If armed → <Name> is holding a <weapon>".
  • Equip the warrior with the weapon.

  • Use composition. The warrior should have a weapon, not inherit from it.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt