EX05 - Basic Functions


In Exercise 05 you will translate your programs from Exercise 03 into well structured programs made of functions.

0. Pull the skeleton code

If you have already pulled the skeleton code for this week’s exercises, no worries about this step. You should already be good to go.

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

If the above did not work, try the following:

  1. Click the Ellipses in the Source Control pane and select “Pull, Push” from the drop-down menu. Then select “Pull from”. Then select “upstream” and the main option. This will begin the pulling process from the course repository. It should silently succeed.
  2. Return to the File Explorer pane and open the exercises directory. You should see it now contains another directory named ex05. If you expand that directory, you should see the starter files for the two Python programs in this exercise

After pulling the skeleton code, above, you can find the starter code for all the functions in the file exercises/ex05/fortune_cookie.py.

We have provided the skeleton of a main function, discussed in lecture on Wednesday 5/26, and have denoted 2x TODO comments for you to complete in order.

First, find TODO 1 and notice it is at the “top-level” of the file (no indentation). Your job here is to define a function named fortune_cookie. It has the following signature expectations:

  1. It should have zero parameters.
  2. It should return a str value.

The str it returns should be a single, random fortune string. You can reuse your logic from ex03, but you will need to be careful with two particular changes:

  1. The logic be indented such that it is a part of the fortune_cookie function’s body block.
  2. You will need to return the generated str value. Do not print the fortune from within the function!

Second, find TODO 2 inside of the main function’s body. Replace this comment with a statement that prints the result of calling the fortune_cookie function you defined below. Hint: function calls must always have a pair of parentheses () following the name of the function, even if there are no arguments to pass.

You should now be able to run your program:

python -m exercises.ex05.fortune_cookie

Requirements

  • This function should return just the one-line fortune. This is the second line of output from ex01. The fortune_cookie function must not have any calls to print inside of it.
  • Only make one call to randint and store the result in a variable
  • Use nested if/else conditional statements within else blocks to control which fortune is returned. In other words, do not use four linear, unrelated if statements.

2. Tar Heel Arithmetic

The next program involves the same small logical and arithmetic puzzle from ex03. You will notice the user is asked for an int in exercises/ex05/tar_heels.py and their response is stored in the variable choice within the main function’s frame. You can also reuse your logic from ex03

First, find TODO 1 and define a function named tar_heels. Its signature has the following additional expectations:

  1. A single parameter of type int. You can decide an appropriate name for it!
  2. It should return a str value.

The body of the tar_heels function should meet the following criteria, which you implemented in the previous exercise:

  • When response is divisible by 2, return “TAR”.
  • When response is evenly divisible by 7, return “HEELS”.
  • When response is evenly divisible by both 2 and 7, return “TAR HEELS” instead of just “TAR” or “HEELS”
  • When none of the above conditions are met, return “CAROLINA”

Second, after defining the tar_heels function, find TODO 2 in the main function. Replace this comment with a statement that prints the value returned by calling tar_heels and giving the user’s choice as an argument to the function call.

Once your function definition and function call are in place at their respective TODOs, you should be able to run your program:

python -m exercises.ex05.tar_heels

3. Make a Backup Checkpoint “Commit”

As you make progress on this exercise, making backups is encouraged. Note that you do not have to make a backup in order to submit your work, though you are encouraged to before each submission so that you can revert back to a previous point in your project if you accidentally change something you did not intend to.

  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 2” 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 “EX05 - Basic Functions”. 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”.

To produce a zip file for ex05, type the following command (all on a single line):

python -m tools.submission exercises/ex05

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