Exercise: More on classes
These exercises will give you practice with class variables and class methods.
Missing class variables
In the first two exercises, the class definitions are missing a class variable. Read the docstring and doctests to determine the name and value of the missing class variable, add it in, and run the doctests after.
Exercise: StudentGrade
📺 Need help with this one? Watch a video walkthrough of the solution. Then try it out yourself and apply what you learned to the next exercise.
class StudentGrade:
"""Represents the grade for a student in a class
and indicates when a grade is failing.
For all students, 159 points is considered a failing grade.
>>> grade1 = StudentGrade("Arfur Artery", 300)
>>> grade1.is_failing()
False
>>> grade2 = StudentGrade("MoMo OhNo", 158)
>>> grade2.is_failing()
True
>>> grade1.failing_grade
159
>>> grade2.failing_grade
159
>>> StudentGrade.failing_grade
159
>>>
"""
def __init__(self, student_name, num_points):
self.student_name = student_name
self.num_points = num_points
def is_failing(self):
return self.num_points < StudentGrade.failing_grade
Exercise: Article
class Article:
"""Represents an article on an educational website.
The license for all articles should be "CC-BY-NC-SA".
>>> article1 = Article("Logic", "Samuel Tarín")
>>> article1.get_byline()
'By Samuel Tarín, License: CC-BY-NC-SA'
>>> article2 = Article("Loops", "Pamela Fox")
>>> article2.get_byline()
'By Pamela Fox, License: CC-BY-NC-SA'
"""
def __init__(self, title, author):
self.title = title
self.author = author
def get_byline(self):
return f"By {self.author}, License: {self.license}"
Class methods
Exercise: Thneed Factory
For this exercise, you'll finish the implementation of make_thneed
, a class method that can make Thneeds of a given size and a random color.
Hint: You can use random.choice(list)
to generate a random color from a list of colors.
📺 Need help with this one? Watch the video walkthrough of the solution. Then try it out yourself and apply what you learned to the next exercise.
import random
class Thneed:
def __init__(self, color, size):
self.color = color
self.size = size
@classmethod
def make_thneed(cls, size):
"""
Returns a new instance of a Thneed with
a random color and the given size.
>>> rand_thneed = Thneed.make_thneed("small")
>>> isinstance(rand_thneed, Thneed)
True
>>> rand_thneed.size
'small'
>>> isinstance(rand_thneed.color, str)
True
"""
# YOUR CODE HERE
Exercise: Cat adoption
For this exercise, you'll finish the implementation of adopt_random_cat
, a class method that can generate new cats. Just what we need in the world! 🐈⬛🐈
Hint: You can use random.choice(list)
to generate a random name from a list of names and random.randrange(start, stop)
to generate a random number of lives.
import random
class Cat:
def __init__(self, name, owner, lives=9):
self.name = name
self.owner = owner
self.lives = lives
@classmethod
def adopt_random_cat(cls, owner):
"""
Returns a new instance of a Cat with the given owner,
a randomly chosen name and a random number of lives.
>>> randcat = Cat.adopt_random_cat("Ifeoma")
>>> isinstance(randcat, Cat)
True
>>> randcat.owner
'Ifeoma'
"""
# YOUR CODE HERE