EX02 - More Variables and Logic


Exercise 02 consists of one program, a vaccine calculator, and will give you some practice with variables. You will also be introduced to datetime objects and get some practice with these.

0. Pull the skeleton code

You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:

  1. 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.
  2. 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.
  3. Click the Ellipses in the Source Control pane and select “Pull” from the drop-down menu. This will begin the pulling process from the course repository. It should silently succeed.
  4. Return to the File Explorer pane and open the exercises directory. You should see it now contains another directory named ex02. If you expand that directory, you should see the starter files for the Python programs in this exercise.

1. Vaccine Calculator

At the current rate of vaccinations, on what date will 80% of the population have received two vaccine shots? Let’s write a program to find out!

Your vaccine_calc.py program will take a few specific inputs, such as these which approximate where the United States is at on May 17th, 2021.

$ python -m exercises.ex02.vaccine_calc
Population: 330000000
Doses administered: 274000000
Doses per day: 1830000
Target percent vaccinated: 80
We will reach 80% vaccination in 139 days, which falls on October 04, 2022.

Notice we can plug in numbers for other populations, as well, such as North Carolina’s.

$ python -m exercises.ex02.vaccine_calc
Population: 10500000
Doses administered: 7700000
Doses per day: 34067
Target percent vaccinated: 80
We will reach 80% vaccination in 267 days, which falls on February 09, 2022.

Finally, here is a mock data set for simple calculations you can test your formulas with:

$ python -m exercises.ex02.vaccine_calc
Population: 100
Doses administered: 50
Doses per day: 2
Target percent vaccinated: 50
We will reach 50% vaccination in 25 days, which falls on June 12, 2021.

The “model” you will use in this calculator will be very naive relative to the more robust vaccination calculators you will find on-line. However, it is still a useful exercise in taking inputs from users and performing arithmetic calculations that go between multiple data types: ints, floats, and even dates! Your vaccine_calc.py will introduce you to two new data types, datetime for modeling a date, and timedelta for modeling a timespan (such as 14 days or 1 hour and 30 minutes). Don’t worry, we will explain what you need to know shortly.

The model will make the following assumptions:

  1. Constant number of doses administered per day
  2. Ignores the pipeline problem of a second dose following the first by one month. We will assume every two doses fully vaccinates one person. To simplify things, we will also not take into account the single dose vaccine. (Johnson & Johnson)

Your calculator should prompt the user for the following integer inputs:

  1. Population - the total number of people under consideration
  2. Doses administered - the total number of vaccine doses already given to the population (remember: 2x doses covers 1x person in the population)
  3. Doses per day - the total number of vaccine doses given each calendar day moving forward (remember: assume this is constant)
  4. Target percent vaccinated - an int input, out of 100, that represents the total percent of population your calculation is targetting (remember: unless doses administered is 0, some amount of progress toward this target has already been achieved)

The output of your calculator should be formatted as shown in the examples above. You will need to concatenate a string to produce the final output.

Working with datetime and timedelta objects

Just like int, str, float, and bool are data types, so are datetime and timedelta. These types are not considered “built-in”, though, and must be imported as shown in the skeleton code of your exercise. These are powerful types which have capabilities beyond our needs, so the following examples will show you what you need to know in order to create your calculator. You are encouraged to play around with these examples in an interactive Python REPL:

$ python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> from datetime import datetime, timedelta

>>> today: datetime = datetime.today()

>>> print(today.strftime("%B %d, %Y"))
May 18, 2021

>>> one_day: timedelta = timedelta(1)

>>> tomorrow: datetime = today + one_day

>>> print(tomorrow.strftime("%B %d, %Y")) 
May 19, 2021

>>> fortnight: timedelta = timedelta(7 + 7)

>>> future: datetime = today + fortnight

>>> print(future.strftime("%B %d, %Y"))
June 01, 2021

>>> quit()

Let’s break down what transpired:

  1. from datetime import datetime, timedelta - This statement imports the definitions of the types datetime and timedelta. This step is taken care of for you already in the skeleton code. We will discuss importing in more depth soon.
  2. today: datetime = datetime.today() - This statement establishes a variable named today whose time is a datetime. The datetime type has a special method, also named today, which when called will evaluate to the current date and time of your computer.
  3. today.strftime("%B %d, %Y") - The today object has a method named strftime, short for “string format (a) time”, because it is a datetime object. This method evaluates to a str from a datetime object with what are called format codes. The %B format code gives you the name of the month, the %d format code is the day of month, and %Y is the 4-digit year. These are the only format codes you will need for this assignment, but if you are curious of what other codes exist, here is the official documentation with the complete list.
  4. one_day: timedelta = timedelta(1) - This statement establishes a variable named one_day whose type is timedelta. The expression timedelta(1) constructs a new timedelta object representing the duration of 1 day.
  5. tomorrow: datetime = today + one_day - This statement establishes a variable named tomorrow, of type datetime, that is assigned the result of adding today, which is today’s date, with one_day, which represents a timespan of a complete day. You can perform addition and subtraction with datetime and timedelta objects in order to calculate dates offset by some time span.
  6. tomorrow.strftime("%B %d, %Y") - Once again, producing a str representation of a datetime object. Notice the result is the day after the earlier example.
  7. fortnight: timedelta = timedelta(7 + 7) - Similar to the previous construction of a timedelta object, this time around notice the argument is an int expression. Hint: It could also be an int variable access expression! A fortnight is 14 days.
  8. future: datetime = today + fortnight - Just like the previous example, except this one is calculating a date 14 days after today.

With that, you should be able to do the date calculations needed.

Hint: Before you begin!

Before you attempt to write this program you are encouraged to write down the steps you would take to calculate the number of days remaining yourself. Try and derive a series of formulaic steps, broken down step-by-step, before you attempt to write this as a Python program. It may be helpful to first write out the process for a specific set of inputs before writing the algorithm for the general case. We also recommend breaking out the pen and paper first if you’re having trouble. It’s always easier to write code once you have the specific ideas expressed in English first.

Let’s take the third example above where population = 100, doses administered = 50, doses per day = 2, and target = 50. If the population is 100 and we need 2 doses to fully vaccinate each individual, then we will need to administer 200 doses to vaccinate 100% of the population. If our target is to vaccinate 50% (0.5) of the population, then we will need to administer a total of 100 doses. Since we have already administered 50 doses, we only need to administer 50 more doses to reach our target. Given that we can administer 2 doses per day, this means that we will be able to administer 50 doses and reach our target in 25 days.

In your program you will need to convert between int and float and back again in some places. When you convert from float back to an int, please use the built-in round function. Think carefully about which steps in the process will yield a float that you want to round. For example, does it make sense to require a non-integer number of doses to reach our target? Additionally, we are only concerned about which day we reach our target (not the time of day), so we will also want to round our calculated number of days until the target is met.

Requirements

Choose meaningful variable names (not just x, y, z, x1, x2, etc) that are descriptive of their purpose. For example, if you were writing a weather program and had a variable that represented the current temperature outside, choosing a name like current_temperature is significantly easier to understand than a name like z.

Break your solution down into steps where necessary to avoid single lines of code becoming overly complex and obtuse.

2. Make a Backup Checkpoint “Commit”

As you make progress on this exercise, making backups is encouraged.

  1. Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
  2. Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
  3. 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.
  4. 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 1” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.

3. Submit to Gradescope for Grading

Login to Gradescope and select the assignment named “EX02 - Variables Part 2.”. 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”.

Type the following command (all on a single line):

python -m tools.submission exercises/ex02

In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex02.zip”. 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. For this exercise there will be 10 points manually graded for style – using meaningful variable names and snake_case. 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!