Editor

Build a Player Class with Score Tracking

Intermediate · Write the code · from Python Classes and Objects: OOP from the Ground Up

From the lesson: Python Classes and Objects: OOP from the Ground Up

Imagine you own a bakery and you have a cookie cutter shaped like a star. Every time you press it into dough, you get a new star-shaped cookie. The cutter itself isn't a cookie - it's a blueprint for making cookies.

Read the lesson

Create a Player class with:

  • A class attribute total_players starting at 0
  • __init__ takes name and sets self.score = 0, and increments Player.total_players
  • A win_round(self, points) method that adds points to self.score
  • Create two players 'Alice' and 'Bob'. Alice wins a round with 10 points, then Bob wins with 15 points. Print each player's score and the total number of players in this exact format:

    Alice: 10
    Bob: 15
    Total players: 2
    # Create your Player class here

    More Classes and Objects exercises

    All Python exercises · 50 practice problems · Object-Oriented Programming quiz