CS21 Lab 3: if statements, for loops
Due Sunday, February 16, before midnight
Goals
The goals for this lab assignment are:
-
Solve programming problems with
if-elif-else
statements. -
Properly print formatted output to the terminal.
-
Practice using the accumulator pattern.
As you write programs, use good programming practices:
-
Use a comment at the top of the file to describe the purpose of the program (see example).
-
All programs should have a
main()
function (see example). -
Use variable names that describe the contents of the variables.
-
Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.
-
Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.
-
Avoid writing any lines of code that exceed 80 columns.
-
Always work in a terminal window that is 80 characters wide (resize it to be this wide)
-
In
vscode
, at the bottom right in the window, there is an indication of both the line and the column of the cursor.
-
Function Comments
All functions should have a top-level comment! Please see our function example page if you are confused about writing function comments.
1. Password generator
With the proliferation of apps, we all need to keep track of way too many passwords! In many cases these passwords must be reset on a regular basis, and there are strict rules about the proper format of a password. For example, passwords must often contain at least one number and at least one special character. It can be difficult to come up with memorable passwords. This program may help.
Security experts recommend using a password manager rather than reusing passwords, which is one of the most common security vulnerabilities. |
Write a program called password.py
that takes in a phrase and uses
string accumulation to produce an encoded version of the phrase that
may be password worthy. This program will remove all spaces from the
phrase and convert all vowels based on the table below.
Original letter | Converted letter |
---|---|
A or a |
@ |
E or e |
3 |
I or i |
1 |
O or o |
0 |
U or u |
^ |
1.1. Sample output
Below are some examples of the running program. User input is shown in bold.
$ python3 password.py Enter string: Swarthmore College Possible password: Sw@rthm0r3C0ll3g3
$ python3 password.py Enter string: my dog Cosmo Possible password: myd0gC0sm0
If the phrase entered by the user contains no spaces and no vowels, then the encoded version of the string will be exactly the same as the original. Add a check to your program for this possibility. The final version of the program should work as follows:
$ python3 password.py Enter string: cs21 Your encoded phrase is the same as the original--try something else!
$ python3 password.py Enter string: I love ice cream Possible password: 1l0v31c3cr3@m
1.2. Requirements
Your program should meet the following requirements:
-
Ask the user to enter a string for generating a password.
-
Convert this string into a possible password by removing all spaces and replacing every vowel according to Table 1.
-
When the possible password is different from the original string, you should print it, otherwise you should report that they are the same and suggest that they try a different string.
2. Movie ticket sales
Write a program called movies.py
that prompts the user for a number of movie tickets to sell and asks for the ages of the ticket buyers. Ticket prices are based on the chart below. Your program should print the cost of each ticket and the total. You may assume the ages and number of tickets entered by the user are valid non-negative integers.
Type | Age | Price |
---|---|---|
Child |
12 and under |
$10 |
Adult |
13 to 59 |
$15 |
Senior |
60+ |
$13 |
2.1. Sample output
Here is a sample run of the program:
$ python3 movies.py Welcome to the movie theater sales kiosk How many movie tickets would you like? 4 Enter the age of the next guest: 7 A child ticket is $10 Enter the age of the next guest: 73 A senior ticket is $13 Enter the age of the next guest: 76 A senior ticket is $13 Enter the age of the next guest: 39 An adult ticket is $15 Your total amount for 4 tickets is $51
2.2. Requirements
Your program’s output should match the example shown above when given the same input, and should meet the following requirements:
-
Use a
for
loop to determine the age of each guest and to accumulate the total cost. -
Report the total cost of the tickets at the end of the program.
3. Compatibility survey
Write a program called survey.py
that asks the user a series of
questions about their favorite things, compares their answers to your
personal preferences, and summarizes the number of answers you have in
common.
3.1. Sample output
An example run of the program might look like this:
$ python3 survey.py Let's take a compatibility survey. What is your favorite color? teal That is my favorite color too! What is your favorite food? ice cream That is my favorite food too! What is your favorite season? spring That is my favorite season too! What is your favorite holiday? spring break spring break is nice, but I prefer thanksgiving Our compatibility score is: 3 out of 4 We have some faves in common.
Here’s another sample run using a different set of questions and preferences:
$ python3 survey.py Let's take a compatibility quiz! What is your favorite veggie? corn corn is nice, but I prefer green beans What is your favorite dessert? pie pie is nice, but I prefer chocolate What is your favorite month? April April is nice, but I prefer March What is your favorite college? Grinnell Grinnell is nice, but I prefer Swarthmore Our compatibility score is: 0 out of 4 We should try out each other's favorites!
3.2. Requirements
-
Your program must use a
for
loop to ask the user questions one at a time, comparing the user’s answers to your preferences, and accumulating a compatibility score. -
Your topics should be stored as a list of strings:
topics = ["color", "food", "season", "holiday"]
-
You must have four topics, but they need not match these. Get creative!
-
All questions will have the same format:
What is your favorite <topic>?
-
Your preferences should also be a list of strings:
preferences = ["teal", "ice cream", "spring", "thanksgiving"]
-
When the user is done, your program should output the number of matches, plus a suitable message.
-
You must have at least three different messages: one for when all preferences match, one for when no preferences match, and one for something in between the two extremes. For example you might say:
-
For 4 matches:
We are really in synch!
-
For 0 matches:
Difference is the spice of life!
-
For any score in between:
We have some faves in common.
-
4. Answer the Questionnaire
After each lab, please complete the short Google Forms questionnaire. Please select the right lab number (Lab 03) from the dropdown menu on the first question.
Once you’re done with that, you should run handin21
again.
Submitting lab assignments
Remember to run handin21
to turn in your lab files! You may run handin21
as many times as you want. Each time it will turn in any new work. We
recommend running handin21
after you complete each program or after you
complete significant work on any one program.
Logging out
When you’re done working in the lab, you should log out of the computer you’re using.
First quit any applications you are running, including your vscode editor, the browser and the
terminal. Then click on the logout icon ( or
) and choose "log out".
If you plan to leave the lab for just a few minutes, you do not need to log
out. It is, however, a good idea to lock your machine while you are gone. You
can lock your screen by clicking on the lock icon.
PLEASE do not leave a session locked for a long period of time. Power may go
out, someone might reboot the machine, etc. You don’t want to lose any work!