Predict the Counter Output
Beginner · Predict the output · 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 lessonRead the code carefully and predict what it prints. Think about what __init__ sets and how increment() changes self.count.
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
return self.count
c = Counter()
print(c.increment())
print(c.increment())
print(c.increment())# Type the exact output you predict:
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
return self.count
c = Counter()
print(c.increment())
print(c.increment())
print(c.increment())
More Classes and Objects exercises
- Create a Book ClassBeginner
- Add a Method to RectangleBeginner
- Fix the Student ClassIntermediate
- Add __str__ to a ProductIntermediate
- Build a Player Class with Score TrackingIntermediate
- Refactor Functions into a ClassAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz