Open the course workspace in VS Code (File > Open Recent > comp110-workspace) and open the File Explorer pane. Expand
Right click on the word
exercises and select “New Folder”.
Name the folder exactly:
Let’s write a program that will boost your spirits, gas you up, and get you hype for the road ahead.
Right click on the
ex01 folder you setup above and select “New File”, name the file
A new capability you will learn in this exercise is the ability to ask for
input from the person using your program. Try adding the following lines of code:
Save the program and try running it in the terminal:
python -m exercises.ex01.hype_machine
input function is evaluated, you will be prompted to enter your name and then see the rest of the program continue as soon as you enter your name and press Enter. Notice the
input function is an expression that will evaluate to whatever the user typed in.
In recent lessons you explored programming in an interactive Python session called a REPL. The REPL acronym describes its behavior, it reads a line of code after you press enter, evaluates the code you entered, and if you entered an expression then its evaluation is printed, before then looping back to reading.
Notice printing evaluated expressions automatically in your terminal happens automatically without you needing to give any special instruction.
Unlike the REPL, when a stored program is evaluated it will not automatically print the evaluation of expressions to your terminal. If you would like the evaluation of an expression to be displayed as output in the terminal, you must call the
In the evaluation of this code listing, you would not see the string literal expression
"You entered: ", nor the variable access expression
name appear in your terminal. If you evaluated these same lines in a REPL, though, you would because of the automatic
The REPL is made for programmers to experiment in, so it has this automatic printing feature for convenience. Not having automatic printing in a stored program allows you far greater control over the output your programs display to someone using your program. Having this control enables you to design programs that are more user friendly than if there were a lot of excessive, extraneous printed outputs and nothing you could do about it.
Key takeaway: if you want the evaluation of an expression in your stored program to display to the user, you must provide it as an argument to the
print("Hello, " + "world!")
The functional requirements of
exercises/ex01/hype_machine.py are to print three lines and use the concatenation operator to construct strings and print them out given the name entered. For a reminder of how to concatenate two
str expressions, refer to Lesson 3
One line you
Another line should end with the name (optionally followed by exclamation points or a period), such as, “Keep slaying Kaki!”
A final line should contain the name entered in the middle, such as “You know what Kaki… you can do it!”
These three lines can be printed in any order.
Here is an example run of a completed program:
What is your name? Marc Yes, that's right, Marc, you are a boss. Marc did you know you are going to crush it? Go forth and have a wonderful day Marc!
The three messages you
Once your program is working, add a docstring at the top of your file with a one-sentence description of your program.
Then, you should add a line with a special variable named
__author__ assigned to your name and e-mail address, the same as you did in exercise 0.
This exercise’s second program is a basic rate of growth calculator for clusters of infections given a basic reproduction number R0, read as “R naught”.
The reproduction number R0 is a measure of how a contagious disease propagates. It represents the average number of people who will contract a disease from one person with that disease. For example, if R0 is 5, it implies 1 person will infect 5 other people. If R0 is 0.5, then then, on average, for every two infected people only one new infection would be expected.
Of course, the R0 of a disease is not a constant and depends on many factors that vary over time including: the population density in question, the precautionary actions and behaviors of a population, and more. Thus the R0 of a disease in a country with neither national leadership nor a coherent strategy for controlling its spread can be much higher than its peer nations.
cluster_funk.py calculator, you will ask the user to enter an R0 value and an intial cluster size at time step 0, t0. You will then compute the number of new infections and total infections for four subsequent time steps. For our purposes, a time step is a simplification and assumes all new infections from the previous round occurred at exactly the R0 rate.
Here is an example run, with an R0 of
2.0 and an intial infected population of
100 at t0:
Enter R0: 2.0 Enter t0 Cluster Size: 100 t1 - New: 200 - Total: 300 t2 - New: 400 - Total: 700 t3 - New: 800 - Total: 1500 t4 - New: 1600 - Total: 3100
Notice the model assumes only the newly infected at a given time step are contagious for the next time stamp. Thus the new cases in tN are calculated based on the new cases in tN − 1 × R0, not the running total.
Here is another example run, with an R0 of
0.2 and an intial infected population of
100 at t0:
Enter R0: 0.2 Enter t0 Cluster Size: 100 t1 - New: 20 - Total: 120 t2 - New: 4 - Total: 124 t3 - New: 1 - Total: 125 t4 - New: 0 - Total: 125
Notice in this example, in the step between t2, where there were
4 new infections, and t3, where there was
1 new infections, involved some rounding. Multiplying t2 by R0, evaluates to
4 * 0.2, which evaluates to
0.8. Thus, you should use the built-in
round function when computing new cases in a time step.
This program will involve working with objects of three data types:
float. To help you check over your work, let’s enable Python Type Checking in VSCode. These are the same rules we’ll check your types with during autograding.
Microsoft’s Pylance VSCode Extension enables type checking for you. Install this plugin, or confirm you installed it during the initial setup process, by going to View > Extensions. Search for Pylance, select it, and install it if it is not already installed. If asked to reload VSCode, go ahead and do so.
Next, open the Command Palette and search for Open Workspace Settings. Select it (not the one that mentions JSON). Search for Python Type Checking and change the setting to strict. This will enable type checking in your workspace from here on out. You can close the settings tab and continue on.
Begin by right clicking on the folder
ex01 in the file explorer and selecting “New File”. Name this one
cluster_funk.py. Double check the spelling and punctuation. You should then be able to run your program with the following terminal command:
python -m exercises.ex01.cluster_funk
For this program, you will need to apply what you learned about
input and concatenation in the previous program, as well as what you learned about using constructors for type conversions in Lesson 4 on Expressions. You will need to convert
str inputs to
float objects before you can compute with them. You will also need to convert
int objects to
str objects before you can concatenate them. You will also need to use the
round function, as described above.
You will have to declare your own variables and, in this program, be sure to explicitly type them as shown in Lesson 7 on Variables. Put some thought into your variables’ names! They should be meaningful and relevant to your program, not merely
The output your program produces in the terminal should exactly match the formatting of this example run shown below, computed results and all:
Enter R0: 0.33 Enter t0 Cluster Size: 1000 t1 - New: 330 - Total: 1330 t2 - New: 109 - Total: 1439 t3 - New: 36 - Total: 1475 t4 - New: 12 - Total: 1487
inputfunction will return a
str, so if you want to be able to do arithmetic with the results, you’ll need to convert the result of the
inputfunction call to a
int, depending on what you need.
cluster_funk.py exercise, we will manually grade your code and are looking for good choices of meaningful variable names. Your variable names should be descriptive of their purposes. We will also manually grade to check that you declared your variables with explicit types.
Once your program is working, add a docstring at the top of your file with a one-sentence description of your program’s purpose.
Then, add your
__author__ variable assigned to your name and e-mail address.