Python by Projects › Checkpoint 4 — Year One Finale
Questions: Checkpoint 4 — Year One Finale
Bring the year's two biggest skills together — OBJECTS and FILES — to build, save, and inspect a tiny hero adventure.
Question 1
Define class Hero: with def __init__(self, name): that sets self.name = name and self.health = 10. Make hero = Hero("Ada"), then print hero.name and hero.health.
How this is checked: Checked by tests that run your code.
Question 2
Include the whole Hero class again so this cell stands alone. Add heal(self, amount) so it does self.health = self.health + amount and returns the new health; make hero = Hero("Ada"), call hero.heal(3), and print the returned value.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 3
Start with scores = [40, 90, 20]. Open "finale.txt" in write mode with a with block, then write each score on its own line as one integer per line.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 4
Start with loaded = []. Open "finale.txt" with a with block, loop with for line in f, append int(line.strip()) to loaded, and print loaded.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 5
Sort loaded from biggest to smallest with loaded.sort(reverse=True), then print the top score with loaded[0]. Do not use max.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 6
Start with inventory = {"sword": 1, "potion": 3}. Loop with for item, count in inventory.items(): and print each item and count.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 7
Start with inventory = {"sword": 1, "potion": 3} and print whether "sword" in inventory as True or False. Then write an if "shield" in inventory: / else: branch that prints inventory["shield"] when it is present and prints "no shield" otherwise.
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Question 8
This code raises KeyError: 'shield' because it looks up a missing key:
inventory = {"sword": 1}
print(inventory["shield"]) # KeyError: 'shield'
Name the LAST line of the traceback as the clue, then rewrite the code so it runs safely: guard the lookup with if "shield" in inventory: and use else to print a default.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.