EX01 - Variables and User Inputs


In this exercise you will use many concepts learned so far in data types, expressions, and variables, in addition to a new capability: asking the person using your program for input!

Setup Exercise Directory

Open the course workspace in VS Code (File > Open Recent > comp110-workspace-…) and open the File Explorer pane. Expand exercises.

Right click on the word exercises and select “New Folder”.

Name the folder exactly: ex01

hype_machine.py

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 hype_machine.py

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

When the 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.

Why is the print function neccessary?

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 print function to do so. You can prove this to yourself by attempting to rewrite the example above as the following:

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 printing.

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 function, such as: print("Hello, " + "world!")

Functional Requirements

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 print should start with the name entered, such as “Kaki is awesome!” if the name entered was the string “Kaki”.

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 print should be uniquely your own hype messages and should not be the same as any of the examples above! You can print additional motivational lines, if you’d like.

Style and Documentation Requirements

Once your program is working, add a docstring at the top of your file with a one-sentence description of your program.

Then, after the docstring but before your program’s code, add an __author__ variable assigned your PID as a str value, the same as you did in exercise 0.

cluster_funk.py

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.

In the 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.

VSCode Type Checking Configuration

This program will involve working with objects of three data types: str, int, and 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.

cluster_funk.py Setup

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 int and 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 need 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 a, b, and c.

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

WARNING: Autograding will very specifically be looking for exactly the format of lines output shown above. When you run the program on your machine with the same inputs as above, your printed results should look exactly as shown.

Tips for cluster_funk

  • The built-in python input function 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 input function call to a float or int, depending on what you need.
  • If you’re seeing output that seems to be very close (maybe your total is 1 off), it’s likely a problem with where you are rounding the numbers.
  • The concatenation operator for strings is designed to work on two string values, and will produce unexpected results if you are not careful. If you are trying to add instead, make sure both of your values are numbers!

Style and Documentation Requirements

For the 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 an __author__ variable assigned your PID as a string value after your docstring.

Make a Backup Commit

  1. Open the Source Control panel: View > SCM.
  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, “Finished Exercise 1!” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…) and select “Push” to send this backed up version to your workspace repository space on GitHub.

Submit to Gradescope for Grading

All that’s left now is to hand-in your work on Gradescope for grading!

Remember, before an assignment’s deadline you can resubmit work as many times as you need to without penalty. Portions of assignments are autograded and will provide near-immediate feedback. We want you to resubmit as many times as it takes you in order to earn full autograding credit!

Login to Gradescope and select the assignment named “EX01 - Variables”. 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, open a new Terminal.

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

python -m tools.submission exercises/ex01

In the file explorer pane, look to find the zip file named “20.mm.dd-hh.mm-submission.zip”. The “mm”, “dd”, and so on, are timestamped 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 hand-graded points for the static types and variable names mentioned above. Thus, you should expect to score 90 out of 100 possible points on this assignment during autograding. 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!