In Exercise 02 you will translate your programs from Exercise 01 into well structured programs made of functions.
Due to the wellness weekend, we are breaking this exercise into two parts. (Each will be considered its own free-standing smaller exercise for final grade tabulation purposes.)
Part 1 is made of two programs and due Friday, 2/12 at 11:59pm (5% early EC by Wednesday, 3% EC by Thursday):
- A good vibes fortune cookie
- A little logical puzzle
Part 2 is a single program and due Thursday, 2/18 at 11:59pm (5% early EC by Tuesday 2/16, 3% EC by Wednesday 2/17):
- A vaccine calculator
0. Pull the skeleton code
You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:
- Be sure you are in your course workspace. Open the file explorer and you should see your work for the course. If you do not, open your course workspace through File > Open Recent.
- Open the Source Control View by clicking the 3-node (circles) graph (connected by lines) icon in your sidebar or opening the command palatte and searching for Source Control.
- Click the Ellipses in the Source Control pane and select “Pull, Push” from the drop-down menu, then select “Pull from…” A box will appear and you should select either “origin” or “upstream”, but not “backup”. This will begin the pulling process from the course repository. It should silently succeed. (If you are on macOS and do not see anything in source control anymore, it may be because of a macOS update. You can typically resolve this by opening a new Terminal, typing the command
xcode-select --install, pressing enter and following its instructions.)
- Return to the File Explorer pane and open the
exercisesdirectory. You should see it now contains two other directores named
ex02_p2. If you expand those directories, you will see the starter files for the three Python programs in this exercise.
Part 1. Fortune Cookie and Tar Heels Arithmetic
Program 1. Fortune Cookie
After pulling the skeleton code, above, you can find the starter code for all the functions in the file
We have provided the skeleton of a
main function, discussed in lecture on Tuesday 2/9, and have denoted 2x
TODO comments for you to complete in order.
TODO 1 and notice it is at the “top-level” of the file (no indentation). Your job here is to define a function named
fortune_cookie. It has the following signature expectations:
- It should have zero parameters.
- It should return a
str it returns should be a single, random fortune string. You can reuse your logic from
ex01, but you will need to be careful with two particular changes:
- The logic be indented such that it is a part of the
fortune_cookiefunction’s body block.
- You will need to
strvalue. Do not
TODO 2 inside of the
main function’s body. Replace this comment with a statement that
fortune_cookie function you defined below. Hint: function calls must always have a pair of parentheses
() following the name of the function, even if there are no arguments to pass.
You should now be able to run your program:
python -m exercises.ex02_p1.fortune_cookie
- This function should return just the one-line fortune. This is the second line of output from ex01. The
fortune_cookiefunction must not have any calls to
- Only make one call to
randintand store the result in a variable
- Use nested
if/elseconditional statements within
elseblocks to control which fortune is returned. In other words, do not use four linear, unrelated
Program 2. Tar Heel Arithmetic
The next program involves the same small logical and arithmetic puzzle from part 2 of ex01. You will notice the user is asked for an
exercises/ex02_p1/tar_heels.py and their response is stored in the variable
choice within the
main function’s frame.
TODO 1 and define a function named
tar_heels. Its signature has the following additional expectations:
- A single parameter of type
int. You can decide an appropriate name for it!
- It should return a
The body of the
tar_heels function should meet the following criteria, which you implemented in the previous exercise:
responseis divisible by 2, return “TAR”.
responseis evenly divisible by 7, return “HEELS”.
responseis evenly divisible by both 2 and 7, return “TAR HEELS” instead of just “TAR” or “HEELS”
- When none of the above conditions are met, return “CAROLINA”
Second, after defining the
tar_heels function, find
TODO 2 in the
main function. Replace this comment with a statement that
tar_heels and giving the user’s
choice as an argument to the function call.
Once your function definition and function call are in place at their respective TODOs, you should be able to run your program:
python -m exercises.ex02_p1.tar_heels
Part 2. Vaccine Calculator
The second part of this exercise is to rewrite the
vaccine_calc.py program as a well-structured program made of functions.
Unlike the programs of Part 1, your original
vaccine_calc program will be subdivided into a main function, which gathers user inputs, and two functions representing parts of the overall computation:
days_to_targetfunction will take the user inputs and return an
intnumber of days until reaching the target with the same logic as
future_datefunction will take an
intnumber of days and return a
strrepresentation of the date that is
intnumber of days from now.
Let’s break down each of these steps, including the specific requirements:
The first natural step of our program from ex01 was to calculate the number of days needed to get the targeted percentage of the population vaccinated.
TODO 1, define a function named
days_to_target. Its signature is expected to meet the following requirements:
- Four parameters, each of type
int, corresponding to the following inputs in this exact order:
target(out of 100)
- The function is expected to return an
The function should compute and return the number of days until the target percentage is vaccinated based on the 4
int parameters. Your model should make the same assumptions from
Once you have implemented your function, at
TODO 2 in the main function, declare a variable to store the result of calling the function you just defined with the user’s inputs.
To test whether you are on the right track, you can add a “debugging print” statement just after the variable declaration to print its contents. A “debugging print” statement is just an ordinary
The second step of this program is getting the projected date that this vaccination goal will be reached. Given an
int representing the number of days to go, your function will return a string in the form of
Month DD, YYYY.
TODO 3, define a function named
future_date. Its signature line will have the following expectations:
- Takes in a parameter of type
intrepresenting the number of days out in the future. Name this parameter anything you’d like.
- Returns a string value.
In the body of the function, you will reproduce the steps you took in the previous exercise to go from an
int number of days to a
str representing a future date. You will once again make use of the
timedelta concepts. Your code will likely be very similar, if not the same, except that perhaps the variable name of your “days” parameter will be different and you will need to
return the formatted date string. Once again, the format string is expected to use the following format:
""%B %d, %Y""
TODO 4, declare a variable that is assigned the result of calling
future_date and giving it the number of days you calculated in the variable above as an argument to the call.
TODO 5, print the expected output by concatenating a string and using the variables you established above. The expected output will look something like the following, with the specific components of % vaccination, days, and specific date replaced based on the program’s calculations:
We will reach 50% vaccination in 25 days, which falls on February 28, 2021.
3. Make a Backup Checkpoint “Commit”
As you make progress on this exercise, making backups is encouraged. Note that you do not have to make a backup in order to submit your work, though you are encouraged to before each submission so that you can revert back to a previous point in your project if you accidentally change something you did not intend to.
- Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
- Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
- Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
- If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
- In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Progress on Exercise 2” will suffice.
- Press the Check icon to make a Commit (a version) of your work.
- Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.
4. Submit to Gradescope for Grading
Login to Gradescope. There will be two assignments for this exercise based on the separate parts with separate deadlines. For each, you’ll see an area to upload a zip file. To produce a zip file for autograding, return back to Visual Studio Code.
If you do not see a Terminal at the bottom of your screen, open the Command Palette and search for “View: Toggle Integrated Terminal”.
To produce a zip file for
ex02_p1, type the following command (all on a single line):
python -m tools.submission exercises/ex02_p1
To produce a zip file for
ex02_p2, type the following command (all on a single line):
python -m tools.submission exercises/ex02_p2
In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex02_p1.zip” (or p2). The “mm”, “dd”, and so on, are timestamps with the current month, day, hour, minute. If you right click on this file and select “Reveal in File Explorer” on Windows or “Reveal in Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.
Autograding will take a few moments to complete. If there are issues reported, you are encouraged to try and resolve them and resubmit. If for any reason you aren’t receiving full credit and aren’t sure what to try next, come give us a visit in office hours!