1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Object-Oriented Programming in Python

Connected

Exercise

Implementing logic for attributes

The Player class you previously created was a good start, but one of the key benefits of class-level attributes is their ability to restrict the upper and/or lower boundaries of data.

In this exercise, you'll modify the Player class definition to restrict the value of position from going above the class-level MAX_POSITION value.

Instructions

100 XP
  • Define the __init__() constructor with two arguments, self and position.
  • Inside the constructor, check if position is less than or equal to the class-level MAX_POSITION; if so, assign position to self.position
  • If position is more than the class-level MAX_POSITION, assign it to the class' .MAX_POSITION attribute.
  • Create a Player object p with a position of 6 and print its MAX_POSITION.