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. ```python 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 """ current_year = 2020 def init(self): self.update() def create(self, kind): "*** YOUR CODE HERE " def update(self): " YOUR CODE HERE " class Coin: def init(self, year): self.year = year def worth(self): " YOUR CODE HERE ***" class Nickel(Coin): cents = 5 class Dime(Coin): cents = 10
时间: 2024-03-03 09:50:09 浏览: 93
Here is the completed code:
```python
class Mint:
"""A mint creates coins by stamping on years.
The update method sets the mint's stamp to Mint.current_year.
"""
current_year = 2020
def __init__(self):
self.update()
def create(self, kind):
coin = kind(self.year)
return coin
def update(self):
self.year = Mint.current_year
class Coin:
def __init__(self, year):
self.year = year
def worth(self):
age = Mint.current_year - self.year
return self.cents + max(0, age - 50)
class Nickel(Coin):
cents = 5
class Dime(Coin):
cents = 10
```
Now we can create instances of the `Mint` class and use them to create coins. Here are some examples:
```python
>>> mint = Mint()
>>> mint.year
2020
>>> dime = mint.create(Dime)
>>> dime.year
2020
>>> dime.worth()
10
>>> Mint.current_year = 2100
>>> nickel = mint.create(Nickel)
>>> nickel.year
2020
>>> nickel.worth()
35
>>> mint.update()
>>> Mint.current_year = 2175
>>> mint.create(Dime).worth()
35
>>> Mint().create(Dime).worth()
10
>>> dime.worth()
115
>>> Dime.cents = 20
>>> dime.worth()
125
```
As you can see, the `Mint` class correctly stamps coins with the current year and calculates their worth based on their age. The `Coin` class and its subclasses also correctly calculate the worth of a coin based on its age.
阅读全文