Starter Files

Download lab09.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the OK autograder.

Submission

By the end of this lab, you should have submitted the lab with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be graded. Check that you have successfully submitted your code on okpy.org. See this article for more instructions on okpy and submitting assignments.

Inheritance

Question 1: Mint

Complete the Mint and Coin classes so that the coins created by a mint have the correct year and worth.

  • Each Mint instance has a year stamp. The update method sets the year stamp to the current_year class attribute of the Mint class.
  • The create method takes a subclass of Coin and returns an instance of that class stamped with the mint's year (which may be different from Mint.current_year if it has not been updated.)
  • A Coin's worth method returns the cents value of the coin plus one extra cent for each year of age beyond 50. A coin's age can be determined by subtracting the coin's year from the current_year class attribute of the Mint class.
class Mint:
    """A mint creates coins by stamping on years.

    The update method sets the mint's stamp to Mint.current_year.

    >>> mint = Mint()
    >>> mint.year
    2020
    >>> dime = mint.create(Dime)
    >>> dime.year
    2020
    >>> Mint.current_year = 2100  # Time passes
    >>> nickel = mint.create(Nickel)
    >>> nickel.year     # The mint has not updated its stamp yet
    2020
    >>> nickel.worth()  # 5 cents + (80 - 50 years)
    35
    >>> mint.update()   # The mint's year is updated to 2100
    >>> Mint.current_year = 2175     # More time passes
    >>> mint.create(Dime).worth()    # 10 cents + (75 - 50 years)
    35
    >>> Mint().create(Dime).worth()  # A new mint has the current year
    10
    >>> dime.worth()     # 10 cents + (155 - 50 years)
    115
    >>> Dime.cents = 20  # Upgrade all dimes!
    >>> dime.worth()     # 20 cents + (155 - 50 years)
    125
    >>> m = Mint()
    >>> n = m.create(Nickel)
    >>> n.worth()
    5
    >>> n.year = 2015
    >>> n.worth()
    115
    """
    current_year = 2020

    def __init__(self):
        self.update()

    def create(self, kind):
"*** YOUR CODE HERE ***"
return kind(self.year)
def update(self):
"*** YOUR CODE HERE ***"
self.year = Mint.current_year
class Coin: def __init__(self, year): self.year = year def worth(self): "The worth is a coin's face value + 1 cent for each year over age 50."
"*** YOUR CODE HERE ***"
return self.cents + max(0, Mint.current_year - self.year - 50)
class Nickel(Coin): cents = 5 class Dime(Coin): cents = 10

Use OK to test your code:

python3 ok -q Mint

Question 2: Quidditch

It's time for the opening quidditch match of the season! We represent the various positions for players with the QuidditchPlayer class and its subclasses. Every player begins with a base_energy level, but every position requires a different proportion of energy. Fill in the energy method for the Beater, Chaser, Seeker, and Keeper classes, according to their docstrings. In addition, fill in the __init__ method for the Chaser class.

class Player:
    def __init__(self, name, base_energy):
        """
        Players have a name, and begin with base_energy.
        """
        self.name = name
        self.base_energy = base_energy

    def energy(self):
        return self.base_energy
class Beater(QuidditchPlayer):
    role = "bludgers"

    def energy(self, time):
        """
        Returns the amount of energy left after playing for time minutes. 
        After playing for time minutes, Beaters lose their base energy level 
        divided by the number of minutes. If time is 0, catch the ZeroDivisionError 
        and print "You can't divide by zero!" instead.
        >>> fred = Beater("Fred Weasley", 640)
        >>> fred.energy(40)
        624.0
        >>> fred.energy(0)
        You can't divide by zero!
        """
"*** YOUR CODE HERE ***"
try: return self.base_energy - (self.base_energy / time) except ZeroDivisionError as e: print("You can't divide by zero!")

Use OK to test your code:

python3 ok -q Beater.energy
class Chaser(QuidditchPlayer):
    role = "score"
    energy_expended = 20

    def __init__(self, name, base_energy, goals):
        """
        Chasers have a name, score goals, and begin with base_energy.
        """
"*** YOUR CODE HERE ***"
self.name = name self.base_energy = base_energy self.goals = goals
def energy(self, time): """ Returns the amount of energy left after playing for time minutes. For every goal they score, they use energy_expended units of energy. In addition, they also use 10% of energy_expended if the number of minutes they have played is a multiple of 9. >>> katie = Chaser("Katie Bell", 230, 2) >>> katie.energy(20) 190 >>> ginny = Chaser("Ginny Weasley", 400, 3) >>> ginny.energy(45) 338.0 """
"*** YOUR CODE HERE ***"
energy = self.base_energy if time % 9 == 0: energy = energy - (0.1 * Chaser.energy_expended) energy = energy - (self.goals * Chaser.energy_expended) else: energy = energy - (self.goals * Chaser.energy_expended) return energy

Use OK to test your code:

python3 ok -q Chaser.energy
class Seeker(QuidditchPlayer):
    role = "snitch"
    energy_expended = 5

    def energy(self, time):
        """
        Returns the amount of energy after time minutes. Seekers expend energy_expended 
        units of their energy for every minute they have been playing.
        >>> harry = Seeker("Harry Potter", 700)
        >>> harry.energy(30)
        550
        """
"*** YOUR CODE HERE ***"
return self.base_energy - (time * Seeker.energy_expended)

Use OK to test your code:

python3 ok -q Seeker.energy
class Keeper(QuidditchPlayer):
    role = "guard"
    energy_expended = 50

    def energy(self, time):
        """
        Returns the amount of energy after time minutes. If less than 30 minutes have 
        passed, then Keepers do not lose any energy. If 30 minutes or more have passed, 
        then Keepers expend 80% of their energy_expended units for every full 15 
        minutes that pass.
        >>> oliver = Keeper("Oliver Wood", 380)
        >>> oliver.energy(45)
        260.0
        """
"*** YOUR CODE HERE ***"
energy = self.base_energy if time < 30: return self.base_energy else: for i in range(time // 15): energy = energy - (0.8 * Keeper.energy_expended) return energy

Use OK to test your code:

python3 ok -q Keeper.energy

After you finish implementing the QuidditchPlayers, run the following command in your terminal to play the game:

python3 -i quidditch_game.py

Submit

Make sure to submit this assignment by running:

python3 ok --submit