EX01 - Input and Variables


In this exercise you will use the concepts learned today (data types, variables, and expressions) to produce two little programs.

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 ex01. If you expand that directory, you should see the starter files for the two Python programs in this exercise.

1. hype_machine.py

Let’s write a program that will boost your spirits, gas you up, and get you hype for the road ahead.

Open up the file in your ex01 directory titled hype_machine.py

A capability you will review 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 this command python -m exercises.ex01.hype_machine. 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.

Functional Requirements

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:

    $ python -m exercises.ex01.hype_machine
    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!

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, add an __author__ variable assigned your PID as a string value as you did in exercise 0.

2. numeric_operators.py

The second program in today’s exercises involves practicing the numeric operators, type conversions, and string concatenation.

Open up the file in your ex01 directory titled numeric_operators.py

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:

    $ python -m exercises.ex01.numeric_operators
    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:

    $ python -m exercises.ex01.numeric_operators
    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

You can run your program to test it as you with the following command: python -m exercises.ex01.numeric_operators 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 ints.

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.

Style and Documentation Requirements

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.

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

4. Submit to Gradescope for Grading

Login to Gradescope and select the assignment named “EX01 - Variables Part 1.”. 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/ex01

In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex01.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!