In this exercise you will use the concepts learned today (data types, variables, and expressions) to produce two little programs.
Open the course workspace in VS Code (File > Open Recent > comp110-workspace-…) and open the File Explorer pane. Expand comp110
> exercises
.
Right click on the word exercises
and select “New Folder”.
Name the folder exactly: ex01_variables
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 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 with Run Without Debugging. You should 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.
The functional requirements of this program are to print three lines and use concatenation to build up strings and print them out given the name entered.
One line should start with the name, 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 periods), such as, “Keep slaying Marlee!”, if the name entered was the string “Marlee”.
A final line should contain the name entered in the middle, such as “You know what Ezri… you can do it!”, if the name entered was the string “Ezri”.
These three lines can be printed in any order.
So here’s 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!
Your three printed lines should be uniquely your hype messages and should not be the same as the examples above!
Once your program is working, add a docstring at the top of your file with a one-sentence description of your program.
Then, add an __author__
variable assigned your PID as a string value as you did in exercise 0.
The second program in today’s exercises involves practicing the numeric operators, type conversions, and string concatenation.
Begin by right clicking on the folder ex01_variables
in the file explorer and selecting “New File”. Name this one numeric_operators.py
and double check the spelling and punctuation.
Your goal in this program is to allow the user to input two number variables and then to print out messages with a specific format to demonstrate how the four interesting numerical operators work in Python.
Here is an example of what your program should accomplish:
Left-hand side: 7
Right-hand side: 5
7 ** 5 is 16807
7 / 5 is 1.4
7 // 5 is 1
7 % 5 is 2
Here’s another example run of the program:
Left-hand side: 8
Right-hand side: 5
8 ** 5 is 32768
8 / 5 is 1.6
8 // 5 is 1
8 % 5 is 3
The first two lines are the result of using the input
function as in the previous example to ask for the left- and right- hand side of expressions you’ll then compute. You’ll want to store both of those values in separate variables.
The input
function returns a str
value of what the person using the program entered. To use it as a part of the computations which follow, you will need to make use of type conversion expressions so that they are converted to int
values.
The next four lines are printed strings. You will need to build these strings up using concatenation. Correctly building these strings up using concatenation and type conversions as appropriate will initially be trickier than you expect. Try and get just the first line of output, the exponentiation line, working before you continue on.
Hint: Try creating additional variables to store the input numbers converted to int
s.
WARNING: Autograding will very specifically be looking for exactly the format of lines output shown above. There should only be one single space between each of the numbers, operators, the word is, and the result. When you run the program on your machine with the same inputs as above, your printed results should look exactly as shown.
For the numeric_operators.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.
Once your program is working, add a docstring at the top of your file with a one-sentence description of your program.
Then, add an __author__
variable assigned your PID as a string value.
Go ahead and delete any submission zips lingering around in your workspace from the previous exercise.
When you are ready to submit for grading, close out any open Python Debug Console terminals using the Trash Can and then open up a clean, new terminal. The following command will bundle both files in the exercise 01 directory:
python -m tools.submission comp110/exercises/ex01_variables
This should produce a submission timestamped with the current date/time for uploading on Gradescope.