4. Exercises: Modules of the End Times¶
These are the last common exercises of the course. The topics today cover files and code that spans multiple modules.
Common Exercises¶
As usual everyone needs to do these
Warmup Exercise¶
Spiraling Out of a File¶
This exercise continues the development of our a-spiraling code from last exercises. A new feature is to be added: it can draw by reading instructions from a file that contains multiple spirals. Each line in the file has the parameters of one spiral.
Learning goals: Reading from files, and converting file contents to variables.
Goal: A program that reads drawing instructions from a file and uses them along with the existing draw function to draw spirals.
Introduction:
You'll need to draw_spiral function from last week's spiral exercise. Copy it to your code file.
Function specification:
draw_from_file- Parameters:
- filename to read (string)
The function must open a
file
and read its lines that describe spirals. Each line has values in the same order as the draw_spiral function's parameters separated by commas:- spiral's color (color value - either a color name or a hexadecimal color code)
- number of arcs to draw (integer)
- spiral's initial radius (integer)
- radius growth (float)
- pen weight (integer)
The function needs to read these values from each line, convert them to correct types, and then call the draw_spiral function with them. Although pen weight is an optional argument, it is found on every line in the file - you can safely assume that each line will have 5 values.
In general you can freely assume that there are no errors in the file.
Use Examples:
Below is one example of a file that your function should be able to read. There's also a familiar picture that's been drawn based on this file.
Hints
Messages
Give feedback on this content
Was this task useful for learning?
Comments about the task?
Main Exercises¶
Dunkin' Donkeygotchi¶
Some might still remember Tamagotchis or other virtual pets from way past. In this exercise you will implement your own virtual pet - a virtual donkey, of course.
This exercise teaches you how to work with a program made of more than one file, and with code made by someone else. We've included some of the donkeygotchi's modules. Your task is to finish the job by filling gaps in the main program and by creating the user interface module. The internal logic of the bit donkey has been already defined, along with a bunch of constants.
Learning goals: Reading code written by other people and completing it. Implementing your own
modules
. Using dictionaries
and constants
in code. Goal: Make a user interface module for an otherwise functioning donkeygotchi.
Introduction:
This exercise uses two completed
code files
and one half-finished file. The files have the following roles:main.py: Main program module, functions as the program's "entrance" and implements the main loop that glues the program's logic together. This file has some placeholders marked with question marks that you have to fill in.donkeydefs.py: Contains predefinedconstants(like Python'smath.pi) that can be used in other modules of the program.donkeylogic.py: The engine of the program that creates and manages data.
The code files can be found at the end under "Resources"
Unfinished Module:
main.pyThe program cannot be ran before the placeholders in
main.py have been filled in. What you need to do for each can be found out by looking at the donkeylogic and donkeydefs modules.Module Specification:
donkeyinterface.py- Docstring:
"Defines the donkeygothi's user interface."- Functions:
show_state: (details below)prompt_choice: (details below)
1st function specification:
show_state- Parameters:
- donkey's state in a data structure (dictionary)
This function prints the donkey's state, i.e. the values of age, money, satiation, happiness and energy. In addition, if the donkey has retired, a separate print is done to indicate this. The exact strings to use can be found from the examples at the end.
2nd function specification:
prompt_choice- Parameters:
- donkey's state in a data structure (dictionary)
- Returns:
- a valid input made by the user (string)
The function prints the available choices - based on the donkey's state - once, and then proceeds to prompt the user to make a choice until they give an input that is currently valid. If the donkey is not retired, the valid inputs can be found from the CHOICES list in the donkeydefs module; if the donkey is retired, valid inputs are in the RETIRED_CHOICES list. These constants should be used for both printing the available choices, and validating inputs. All strings to use in prompts and prints can be found from the examples.
Use Examples:
The donkey is 96 years old and has 0 eur. Satiation: 5 Happiness: 5 Energy: 5 Choices: q, f, w, t Input next choice: q
The donkey is 96 years old and has 0 eur. Satiation: 5 Happiness: 5 Energy: 5 Choices: q, f, w, t Input next choice: Z Invalid input! Input next choice: ??? Invalid input! Input next choice: w The donkey is 97 years old and has 1 eur. Satiation: 5 Happiness: 5 Energy: 5 Choices: q, f, w, t Input next choice:
The donkey is 100 years old and has 12 eur. Satiation: 6 Happiness: 9 Energy: 2 The donkey has retired. Choices: q, r Input next choice: X Invalid input! Input next choice: f Invalid input! Input next choice: t Invalid input! Input next choice: r The donkey is 0 years old and has 0 eur. Satiation: 5 Happiness: 5 Energy: 5 Choices: q, f, w, t Input next choice:
Resources:
Download these files from below.
"""
Defines the donkeygotchi's internal logic
"""
import donkeydefs as defs
def init():
"""
Initializes donkey data by creating a new dictionary with initial values for
all keys, and returns it.
"""
donkeydata = {
"SATIATION": defs.INITIAL,
"HAPPINESS": defs.INITIAL,
"ENERGY": defs.INITIAL,
"AGE": 0,
"MONEY": 0,
"RETIRED": False,
}
return donkeydata
def _age(donkeydata):
"""
Ages the donkey and - if needed - puts it in retirement. Meant only for
internal use in this module.
"""
donkeydata["AGE"] += 1
if donkeydata["AGE"] == defs.RETIREMENT_AGE:
donkeydata["RETIRED"] = True
def _update_states(donkeydata):
"""
Changes the donkey's state as time passes, and puts the donkey in retirement
if needed. Meant only for internal use in this module.
"""
if donkeydata["AGE"] % 2 == 0:
if donkeydata["SATIATION"] > 6 and donkeydata["ENERGY"] < defs.MAX_STATE:
donkeydata["ENERGY"] += 1
donkeydata["SATIATION"] -= 1
if donkeydata["AGE"] % 3 == 0:
donkeydata["HAPPINESS"] -= 1
if donkeydata["SATIATION"] <= 0 or donkeydata["HAPPINESS"] <= 0 or donkeydata["ENERGY"] <= 0:
donkeydata["RETIRED"] = True
def feed(donkeydata):
"""
Feeds the donkey, i.e. raises its satiation, if it isn't at the maximum yet.
"""
_age(donkeydata)
_update_states(donkeydata)
if donkeydata["SATIATION"] < defs.MAX_STATE:
donkeydata["SATIATION"] += 1
def tickle(donkeydata):
"""
Tickles the donkey, i.e. raises its happiness, it it isn't at the maximum yet.
"""
_age(donkeydata)
_update_states(donkeydata)
if donkeydata["HAPPINESS"] < defs.MAX_STATE:
donkeydata["HAPPINESS"] += 1
def work(donkeydata):
"""
Makes the donkey work by spending its energy in return for money.
"""
_age(donkeydata)
donkeydata["ENERGY"] -= 1
donkeydata["MONEY"] += 1
_update_states(donkeydata)
"""
Defines the variables that are used in the donkeygotchi.
"""
# input choices
QUIT = "q"
FEED = "f"
TICKLE = "t"
WORK = "w"
CHOICES = [QUIT, FEED, TICKLE, WORK]
RESET = "r"
RETIREMENT_CHOICES = [QUIT, RESET]
# Aasin tilat
INITIAL = 5
RETIREMENT_AGE = 100
MAX_STATE = 10
import ???
import ???
import ???
def main():
"""
Creates a new donkey and implements the main menu logic of donkeygotchi.
"""
donkeydata = donkeylogic.???
while True:
donkeyinterface.show_state(donkeydata)
choice = donkeyinterface.prompt_choice(donkeydata)
if choice == donkeydefs.QUIT:
break
if choice == donkeydefs.FEED:
???
elif choice == donkeydefs.TICKLE:
???
elif choice == donkeydefs.WORK:
???
elif choice == donkeydefs.RESET:
???
if __name__ == "__main__":
???
Hints
Messages
Give feedback on this content
Was this task useful for learning?
Comments about the task?
Give feedback on this content
Comments about these exercises