In this exercise you will use the concepts learned in the Lessons from Thursday 9/02 to produce three little programs.
Assignment Outline
- Fortune Cookie (Level: Novice) – 30 Points Autograded
- Repeat Beat (Level: Advanced) – 35 Points Autograded
- Count Letters (On Your Own) – 15 Points Autograded
- Style, Linting, Typing – 20 Points Autograded
0. Pull the skeleton code
You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:
- 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.
- 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.
- 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.
- Return to the File Explorer pane and open the
exercises
directory. You should see it now contains another directory namedex02
. If you expand that directory, you should see the starter files for the three Python programs in this exercise. - If you do not see the
ex02
directory, try once more but selecting"Pull From"
and selectupstream
in step 2.
1. fortune_cookie.py – 30 Points
After pulling the skeleton code, above, you can find the starter code for Fortune Cookie in the file exercises/ex02/fortune_cookie.py
.
Your program is expected to print three lines. The first line of output must be the message Your fortune cookie says...
. The second line of output is random and will be discussed further following the example. The third line of output must be the message Now, go spread positive vibes!
. An example of running this program a few times is shown below.
$ python -m exercises.ex02.fortune_cookie
Your fortune cookie says...
A beautiful, smart, and loving person will be coming into your life.
Now, go spread positive vibes!
$ python -m exercises.ex02.fortune_cookie
Your fortune cookie says...
Your life will be happy and peaceful.
Now, go spread positive vibes!
$ python -m exercises.ex02.fortune_cookie
Your fortune cookie says...
Soon life will become more interesting.
Now, go spread positive vibes!
Randomization
Python has a standard random
library for producing pseudo-random numbers, which is great for our purposes. In the skeleton code, we have already imported a function named randint
for you from the random
library. You can use this function to generate a random int
value within some range of possible numbers. You can play around with randint
by starting an interactive Python REPL and importing it as shown in the skeleton code:
$ python
Python 3.9.5 (tags/v3.9.5:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import randint
>>> print(randint(1, 10))
8
>>> print(randint(50, 100))
76
>>> print(randint(1, 2))
1
>>> print(randint(1, 2))
2
>>> type(randint(1, 10))
<class 'int'>
>>> quit()
The numbers your examples produce will be random and different from the example shown above. Notice the randint
function takes two input arguments, both int
expressions separated by a comma, and evaluates to a single int
value that is a random int
between and inclusive of the two input arguments.
Since the evaluation of a call to randint
is an int
, you can use a variable declaration and assignment statement in your program to store the randomized result. Then, using that variable and nested if/else
conditional statements, you should print one of at least four messages at random.
You can choose any random fortune messages you would like for your program!
Notes:
- Only make one call to
randint
and store the result in a variable - Use nested
if/else
conditional statements withinelse
blocks to control which fortune is printed. In other words, do not use four linear, unrelatedif
statements.
2. repeat_beat.py – 35 Points
After pulling the skeleton code, above, you can find the starter code for Repeat Beat in the file exercises/ex02/repeat_beat.py
.
This program will repeat a chosen word or phrase a specified number of times.
Use the input
function to prompt the user for a beat (whatever string they want to repeat) and a number (however many times they want to repeat the beat).
The printed output should be the beat repeated however many times the user specified, with a space between each instance of the beat.
If the number given by the user is less than or equal to 0, print “No beat…”.
Example program run A:
python -m exercises.ex02.repeat_beat
What beat do you want to repeat? bop
How many times do you want to repeat it? 3
bop bop bop
Example program run B:
python -m exercises.ex02.repeat_beat
What beat do you want to repeat? boom
How many times do you want to repeat it? -1
No beat...
This must be completed using a while loop. Use of any concepts not learned yet in this course are prohibited.
Notes:
- For full credit, you shouldn’t have a space after the final beat. For example, repeating bop 3 times should print “bop bop bop”.
- If you’ve encountered the string multiplication operator before, we don’t want you to use it for this assignment. Use the concatenation operator instead for now.
- Consider having a
str
variable, separate from the beat, that you can add to in a repetitive fashion.
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.
3. count_letters.py ON YOUR OWN – 15 Points
The third program in today’s exercises involves practicing with conditionals and loops and is to be completed on your own with no outside help from anyone, including course staff. Completing the first two parts of the exercise along with the material through 9/2 will set you up to complete this portion of the exercise.
If you have an error or problem that is stopping you from running your program, we are happy to help you get to a point where you can make forward progress.
Open up the file in your ex02 directory titled count_letters.py
Your goal in this program is to allow the user to input two strings, a single letter to search for an arbitrary word, and then to print out the number of times that letter appears in the word.
Here are some examples of what your program should accomplish:
$ python -m exercises.ex02.count_letters
What letter do you want to seach for?: a
Enter a word: California
Count: 2
$ python -m exercises.ex02.count_letters
What letter do you want to seach for?: p
Enter a word: Apple
Count: 2
Your program should be case sensitive. For example:
$ python -m exercises.ex02.count_letters
What letter do you want to seach for?: a
Enter a word: Apple
Count: 0
$ python -m exercises.ex02.count_letters
What letter do you want to seach for?: A
Enter a word: Apple
Count: 1
You can run your program to test it as you with the following command: python -m exercises.ex02.count_letters
This must be completed using a while loop. Use of any concepts not learned yet in this course are prohibited.
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.
4. Make a Backup Checkpoint “Commit”
As you make progress on this exercise, making backups is encouraged.
- Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
- Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
- 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.
- 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.
- Press the Check icon to make a Commit (a version) of your work.
- Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.
5. Submit to Gradescope for Grading
Login to Gradescope and select the assignment named “EX02 - Conditionals and Loops.”. 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/ex02
In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex02.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!