CS21 Lab 3: if/else statements

Due Saturday (Feb 13) before midnight

This lab assignment requires you to write a few programs. First, run update21. This will create the cs21/labs/03 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/03 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/03
$ pwd
/home/your_user_name/cs21/labs/03
We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!

Programming tips

As you write your programs, use good programming practices:


1. Grade calculator

Write a program that asks the user for a numerical test grade on a 100 point scale, then computes the user's letter grade for that test, according to the following scale:

GradePercentage
A90-100
B80-89
C70-79
D60-69
NC0-59

You can assume the user enters an integer. If the score the user enters is not in the range 0-100, output an appropriate message. Here are some sample runs showing how your program should work:

$ python lettergrade.py
What is your score? 87

You earned a B.
$ python lettergrade.py
What is your score? 72

You earned a C.
$ python lettergrade.py
What is your score? 999

I'm sorry, 999 is not a valid score for this test.

2. Compatibility Survey

Write a program, compatibility.py that asks the user 4 questions about what things they like, and then compares them to your preferences. An example run of the program might look like the following:

$ python compatibility.py

 1. What is your favorite food?  pizza
>>>>>>> That is my favorite food too!

 2. Who is your favorite author? John Steinbeck
>>>>>>> John Steinbeck is OK, but I prefer J. R. R. Tolkien.

 3. Who is your favorite band? The Beatles
>>>>>>> I like The Beatles too!

 4. What is your favorite planet? Earth
>>>>>>> I like Earth too!

We have 3 out of 4 preferences the same!
Not bad.

Here are the requirements for this program:


3. Counting repeated characters

A pair of characters in a string is a repeat if they are consecutive and the same character. Write a program called countrepeats.py that asks the user for an input string and then counts how many repeats the string contains. Here are some examples:

$ python countrepeats.py
input string: I like puppies and kittens.
There are 2 repeats in that string.
$ python countrepeats.py
input string: we LOVE computer science!!
There are 1 repeats in that string.
$ python countrepeats.py
input string: HI!!!!!!!
There are 6 repeats in that string.

4. subsequence

A subsequence of a string is a string that can be obtained by deleting some characters without changing the order of the characters. For example, howl is a subsequence of "hello world!", as shown below:

"hello world!"

Note that each character of "howl" appears in "hello world!" in order, but not necessarily consecutively. Write a program that asks the user for two strings: a text string, and a pattern string, and determines if the pattern is a subsequence of the text. Here are some sample runs showing how your program should work:

$ python subsequence.py
What is the text string? hello world!
What is the pattern string? howl
[howl] is a subsequence of [hello world!]
$ python subsequence.py
What is the text string? four score and seven years ago
What is the pattern string? save
[save] is a subsequence of [four score and seven years ago]
$ python subsequence.py
What is the text string? hello world!
What is the pattern string? world hello
[world hello] is not a subsequence of [hello world!]

Hint: If the pattern is a subsequence of the text, then each character of the pattern appears in the text, in order. It will help to search for the characters of the pattern string one at a time. You will need to figure out how to search the text so e.g. the second character of the pattern string appears in the text after the first character, the third character after the second, etc.

Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.