|
| 1 | +<h2 align=center>Lecture 27</h2> |
| 2 | + |
| 3 | +<h1 align=center>Final Exam Review</h1> |
| 4 | + |
| 5 | +### 13 Floréal, Year CCXXXI |
| 6 | + |
| 7 | +***Song of the day***: _[**Mr. Bojangles**](https://youtu.be/LzofnHLOer4) by Nina Simone (2011)_ |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +### _Eeveelutionary Theory_ |
| 12 | + |
| 13 | +As you may remember from the review, the Pokémon [**Eevee**](https://bulbapedia.bulbagarden.net/wiki/Eevee_(Pok%C3%A9mon)) has several type-specific [**evolutions**](https://bulbapedia.bulbagarden.net/wiki/Eeveelution). Eevee will evolve into some of these Pokémon depending on which **evolution stone** it is given. For the purposes of this problem, we'll focus on the original three Eeveelutions: |
| 14 | + |
| 15 | +- **Vaporeon** (_`"water"`-type_): If Eevee is given a _Water Stone_ (`"water_stone"` in code). |
| 16 | +- **Jolteon** (_`"electric"`-type_): If Eevee is given a _Thunder Stone_ (`"thunder_stone"` in code). |
| 17 | +- **Flareon** (_`"fire"`-type_): If Eevee is given a _Fire Stone_ (`"fire_stone"` in code). |
| 18 | + |
| 19 | +Define an `Eevee` class that will accept two parameters: `nickname`, a string containing the nickname given to the `Eevee` object, and `description_filepath`, a string containing the address of a `txt` file containing a short description of this specific `Eevee` object. The file, if it exists, will always look slightly different depending on |
| 20 | +the Eevee's specific stats, but it will always be of the following general format: |
| 21 | + |
| 22 | +```text |
| 23 | +Eevee |
| 24 | +Nature: friendly |
| 25 | +Level: 5 |
| 26 | +``` |
| 27 | + |
| 28 | +The `Eeevee` class will have five instance attributes: |
| 29 | + |
| 30 | +- `nickname`: The nickname passed by the user. |
| 31 | +- `evol_status`: The current Eeveelutionary status of the `Eevee` object. The value of this attribute is `None` when the Eevee is un-evolved, or either `"Vaporeon"`, `"Jolteon"`, or `"Flareon"` if it has been evolved. We can assume that all newly instantiated `Eevee` objects start un-evolved. |
| 32 | +- `type`: A string containing the current type of the `Eevee` object. We can assume that all newly instantiated `Eevee` objects are of type `"normal"`. |
| 33 | +- `nature`: A string containing the nature denoted in the file. Will be `None` if file is not successfully opened. |
| 34 | +- `level`: An integer containing this object's level as denoted in the file. Will be `1` if file is not successfully opened. |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +The `Eevee` class will have three instance methods associated to it. The first `can_evolve()` (_**sig**: `str` => `bool`_), will accept one string parameter, `stone_name`, a string containing the name of the evolutionary stone that the user wishes to give to this `Eevee` object. `can_evolve()` will then return `True` **if and only if**: |
| 39 | + |
| 40 | +1. The `Eevee` object is un-evolved, |
| 41 | +2. The `stone_name` is one of the three valid evolutionary stones mentioned above. |
| 42 | + |
| 43 | +If either of these conditions is not met, `can_evolve()` will return `False`. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +Next, define a method called `evolve()` (_**sig**: `str` => `None`_). `evolve()` will accept one parameter, `stone_name`, a string containing the name of the evolutionary stone that the user wishes to give to this `Eevee` object. If the `Eevee` object meets all conditions necessary to evolve, `evolve()` will update the `evol_status` |
| 48 | +and `type` of the `Eevee` object to their appropriate new values. |
| 49 | + |
| 50 | +To make this a little simpler, you may assume that the following dictionary is already defined at the top of your file, which you may use if you find it useful (in other words, you **don't** have to use it if you don't find it useful): |
| 51 | + |
| 52 | +```python |
| 53 | +INFO_PER_STONE = { |
| 54 | + "water_stone": { |
| 55 | + # Water stone information |
| 56 | + "eeveelution": "Vaporeon", |
| 57 | + "type": "water" |
| 58 | + }, |
| 59 | + "thunder_stone": { |
| 60 | + # Thunder stone information |
| 61 | + "eeveelution": "Jolteon", |
| 62 | + "type": "electric" |
| 63 | + }, |
| 64 | + "fire_stone": { |
| 65 | + # Fire stone information |
| 66 | + "eeveelution": "Flareon", |
| 67 | + "type": "fire" |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +Finally, define a `get_stats()` method (_**sig**: `None` => `list`_) that will return a list of the values of all the object's non-`None` attributes. If you can, do this using list comprehension—only then will you become a Python deity. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +Sample behaviour: |
| 79 | + |
| 80 | +```python |
| 81 | +# If the file exists... |
| 82 | +camille = Eevee("Camille", "description.txt") |
| 83 | +print(camille.get_stats()) # pre-evolution |
| 84 | + |
| 85 | +camille.evolve("thunder_stone") |
| 86 | +print(camille.get_stats()) # post-evolution |
| 87 | + |
| 88 | +# If the file doesn't exist... |
| 89 | +fryderyk = Eevee("Fryderyk", "not_description.txt") |
| 90 | +print(fryderyk.get_stats()) # pre-evolution |
| 91 | + |
| 92 | +fryderyk.evolve("fire_stone") |
| 93 | +print(fryderyk.get_stats()) # post-evolution |
| 94 | +``` |
| 95 | + |
| 96 | +Output: |
| 97 | + |
| 98 | +```text |
| 99 | +['Camille', 'friendly', 5, 'normal'] |
| 100 | +['Camille', 'Jolteon', 'friendly', 5, 'electric'] |
| 101 | +['Fryderyk', 1, 'normal'] |
| 102 | +['Fryderyk', 'Flareon', 1, 'fire'] |
| 103 | +``` |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +[**Solution**](solution/eevee.py) |
0 commit comments