Challenge: create an Item class

You have successfully created a Room class, complete with a constructor, attributes, getters, and setters. If you would like to really test your skills, have a go at creating a class from scratch. The challenge is to create a class to represent an item you might find in the game, such as a sword. If you choose to have a go at this challenge, we will show you how to integrate the Item class into your game during week 4.

Challenge

challengeicon

Create a new Python file, save it as item.py, and create a class called Item inside it. If you’re using Trinket, create a new trinket to keep this separate from your game so far. You can create another tab for Item.py and test the class in main.py.

The Item class should have the following:

  • Attributes for the name and the description of the item
  • A constructor method
  • Getters and setters for the name and the description of the item
  • Any additional attributes and methods you would like to add

Don’t forget to test your Item class by creating an Item object and then calling the methods.

You can see all of the code from week 2, plus an example solution to this challenge, in this trinket.

Share this article:

This article is from the free online course:

Object-oriented Programming in Python: Create Your Own Adventure Game

Raspberry Pi Foundation