CS21 Lab 3: if statements, for loops
Due Saturday, February 15, before midnight
Programming Tips
As you write your first programs, start using good programming practices now:
-
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.
Are your files in the correct place?
Make sure all programs are saved to your cs21/labs/03
directory! Files
outside that directory will not be graded.
$ update21
$ cd ~/cs21/labs/03
$ pwd
/home/username/cs21/labs/03
$ ls
Questions-03.txt
(should see your program files here)
Goals
The goals for this lab assignment are:
-
practice using
if-else
statements -
continue working with
for
loops and accumulators
1. Password generator
According to Microsoft Research, the average user has at least six different 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 |
^ |
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
2. Calculating speeding fines
Write a program called speeding.py
that calculates speeding fines based on the traffic laws of the state of Pennsylvania. Here is a short description of the fine structure:
For most speeding violations, the fine is $35 plus $2 for every mile in excess of 5 mph over the limit. However, if the maximum limit is 65 mph or higher, the fine is $42.50 plus $2 for every mile in excess of 5 mph over the limit.
The goal of this law is to discourage motorists from driving significantly over the posted limits. If the motorist is traveling just slightly over the limit (within 5 mph), then only a base fine is applied. However, if the motorist’s speed exceeds the limit by more than 5 mph, then an extra fine is applied.
See the table below for some example scenarios.
Speed |
Limit |
Over |
Excess > 5 |
Base fine |
Extra fine |
Total fine |
45 |
50 |
0 |
0 |
0.00 |
0.00 |
0.00 |
52 |
50 |
2 |
0 |
35.00 |
0.00 |
35.00 |
65 |
50 |
15 |
10 |
35.00 |
20.00 |
55.00 |
69 |
65 |
4 |
0 |
42.50 |
0.00 |
42.50 |
75 |
65 |
10 |
5 |
42.50 |
10.00 |
52.50 |
Below are some examples of the running program. User input is shown in bold.
$ python3 speeding.py
Enter speed limit: 55
Enter clocked speed: 45
Motorist is within the limit
$ python3 speeding.py
Enter speed limit: 55
Enter clocked speed: 58
Motorist is over the limit by 3 mph
Total fine is: $35.00
$ python3 speeding.py
Enter speed limit: 55
Enter clocked speed: 63
Motorist is over the limit by 8 mph
Base fine is: $35.00
Additional $2 per 3 miles in excess of 5 mph over limit
Total fine is: $41.00
$ python3 speeding.py
Enter speed limit: 65
Enter clocked speed: 69
Motorist is over the limit by 4 mph
Total fine is: $42.50
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. An example run of the program might look like this:
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 are the requirements for this program:
-
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.
-
Some example messages are shown below, but again feel free to personalize them:
-
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. Extra Challenge
This part is optional and does not affect your grade. Only attempt this after completing the rest of your lab. It is simply an extra challenge, if you want to try it.
In the original version of the program, you have to pick a single favorite for each category. However, lots of folks have several favorites. Change the preferences to be a list of lists. Each inner list keeps track of your multiple favorites for a particular category. For example, your preferences might be:
preferences = [["teal", "green", "blue"], ["ice cream"], ["spring", "summer"], ["thanksgiving", "spring break"]]
Modify the program so that it checks whether the user’s favorite for a given category matches any one of your favorites. Hint: You can use python’s in
operator to test whether a given item is stored within a list.
The updated version of the program would run as shown below. Note that the output message has been slightly modified to reflect that there isn’t a single favorite.
Let's take a compatibility survey.
What is your favorite color? green
That is one of my favorite colors too!
What is your favorite food? cake
cake is nice, but I prefer ice cream
What is your favorite season? summer
That is one of my favorite seasons too!
What is your favorite holiday? halloween
halloween is nice, but I prefer thanksgiving
Our compatibility score is: 2 out of 4
We have some faves in common.
5. Answer the Questionnaire
Each lab will have a short questionnaire at the end. Please edit
the Questions-03.txt
file in your cs21/labs/03
directory
and answer the questions in that file.
Once you’re done with that, run handin21
again.
Turning in your labs….
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.