## 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.) Hint: Check out the `create_animal` method in this demo.
• 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):
return kind(self.year)
def update(self):
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."
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!
"""

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.
"""

super().__init__(name, 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
"""

cur_energy = self.base_energy
cur_energy -= self.energy_expended * self.goals # Note: Chaser.energy_expended works too
if time % 9 == 0:
cur_energy -= 0.1 * self.energy_expended
return cur_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
>>> harry.energy(20)
600
"""

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
>>> oliver.energy(29)
380
"""

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``

## Optional List Review

### Question 3: Shopping Tax

Complete the function `tax` which takes in a list that represents a shopping cart called `shopping_cart` and return a new list that also represents the same shopping cart but with a `percent` tax added to the price of each item.

A shopping cart is represented as a list of 3-element tuples like this:

``[(item1, cost1, quantity1), (item2, cost2, quantity2), ..., (itemN, costN, quantityN)]``

Then complete the function `total_cost` which takes in a list that represents a shopping cart called `shopping_cart` and returns the total cost of all the items in that shopping cart.

``````def tax(shopping_cart, percent):
""" Returns a new list where a `percent` tax is added to each item's price in a shopping cart.
>>> fruit_cart = [("apple", 0.5, 3), ("banana", 0.25, 4)]
>>> tax(fruit_cart, 10)
[('apple', 0.55, 3), ('banana', 0.275, 4)]
>>> cal_cart = [("oski", 1000, 1), ("go", 1.25, 2), ("bears", 3.5, 2)]
>>> tax(cal_cart, 100)
[('oski', 2000.0, 1), ('go', 2.5, 2), ('bears', 7.0, 2)]
"""

tax_multiplier= 1 + (percent / 100)
return [(name, price * tax_multiplier, quantity) for (name, price, quantity) in shopping_cart]``````

Use OK to test your code:

``python3 ok -q tax``

### Question 4: Shopping Total Cost

``````def total_cost(shopping_cart):
""" Returns a float that is the total cost of all items in the shopping cart.
>>> fruit_cart = [("apple", 0.5, 3), ("banana", 0.25, 4)]
>>> taxed_fruit = tax(fruit_cart, 10)
>>> total_cost(taxed_fruit)
2.75
>>> cal_cart = [("oski", 1000, 1), ("go", 1.25, 2), ("bears", 3.5, 2)]
>>> taxed_cart = tax(cal_cart, 100)
>>> total_cost(taxed_cart)
2019.0
"""
``python3 ok -q total_cost``
``python3 ok --submit``