CS21 Lab 3: if statements and for loops

Due Saturday, February 16, before midnight

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
syllableCount.py

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.
• 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: test, test again, and then test some more!
• 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.

Topics for this assignment

• For loops
• if statements
• relational operators (e.g. >,<,==,>=,<=)
• logical operators (and, or, not)

Write a program grade.py that converts a numeric grade score into a letter grade, according to the grading scheme below.

> 100 invalid score
90-100 A
80-89 B
70-79 C
60-69 D
0-59 NC
< 0 invalid score

Assume a sharp threshold for grades; e.g. an 89.6 is considered a B.

Here are three examples (user input in bold):

$python3 grade.py Enter final grade: 95 You earned an A!$ python3 grade.py

You earned a D.

$python3 grade.py Enter final grade: 110 This is not a valid grade score. 2. Encryption Cipher Cryptography is the science of secure communication. An encryption algorithm is procedure for encoding a secret message in such a way that the encoded message is unreadable without knowing how the secret was encoded. A cipher is a special class of encryption algorithms that encrypts one letter at a time. In this problem, you will implement a simple cipher which encrypts each letter by shifting or "rotating" the letter 13 places in the alphabet. For example, you will replace the first letter of the alphabet a with n, since n is the fourteenth letter. You'll replace each b with an o, each c with a p, and so on. See the table below for a complete description.  replace a b c d e f g h i j k l m n o p q r s t u v w x y z with n o p q r s t u v w x y z a b c d e f g h i j k l m This cipher is called "ROT13" because it rotates each letter 13 places in the alphabet. This cipher is interesting because encrypting your message a second time returns the original message. Write a program called rot13.py that takes a string and encrypts it using the ROT13 cipher. If the input string contains characters that are not lower case letters, include them in the encrypted string but do not try to encode them. Here are a few examples (user input in bold): $ python3 rot13.py
Enter text to encrypt: hello world!
Your encrypted text is: uryyb jbeyq!

$python3 rot13.py Enter text to encrypt: uryyb jbeyq! Your encrypted text is: hello world!$ python3 rot13.py
Enter text to encrypt: The quick brown fox jumps over the lazy dog
Your encrypted text is: Tur dhvpx oebja sbk whzcf bire gur ynml qbt

Hint: Your solution can be greatly simplified by using the Unicode encoding of letters. The letters a...z are encoded in order. You can use obtain Unicode encoding of a letter and vice versa using the ord() and chr() functions:

• The ord() function returns the integer encoding of a letter. For example, ord('a') returns 97, ord('b') returns 98, etc.
• The chr() function takes an integer encoding of a letter and returns the letter itself. For example, chr(97) returns 'a'; chr(98) returns 'b'; and chr(97+13) returns 'n'.

3. SyllableCount

Write a program syllableCount.py that prompts the user for a word and then outputs an estimate of the number of syllables in that word. One reasonable way to estimate the number of syllables in the word is to divide the word into groups of vowels and consonants, and to count the number of vowel groups.

Say that a vowel group is a series of consecutive letters each one of which is a vowel; i.e., a letter in the list [a,e,i,o,u]. Similarly, say that a consonant group is a series of consecutive letter each one of which is a consonant. For example, counter has two vowel groups---"ou", and "e".

Your program should prompt the user for a word, and output an estimate of the number of syllables in the word, subject to the following:

• the number of syllables will equal the number of vowel groups in the word, except that
• each word will have at least one syllable. For example, "why" has a syllable, despite not having any vowel groups.
• when the word ends in an e and is immediately preceded by a consonant group, assume the e is silent and is not a syllable.

You can assume that the user enters a single word and all letters are lower-case.

Here's a few quick examples (user input in bold):

$python3 syllableCount.py Enter your word: chair chair has one syllable.$ python3 syllableCount.py
blueberries has 3 syllables.

\$ python3 syllableCount.py
swarthmore has 2 syllables.

-------------- Steps for syllableCount.py --------------

We recommend the following incremental development steps to gradually build up your syllable count program:

1. First, write some python code that determines whether or not a letter is a vowel.

2. Second, write a program that counts the number of vowel groups in a word. One way to do this is to scan through all the letters in the word, checking whether the current letter is a vowel or consonant. Note: to determine the number of vowel groups, you'll need to keep track both of whether the current letter is a vowel, and whether or not the previous letter is a vowel.

3. Third, add in a special case that checks whether there are no vowel groups, and if so, outputs one syllable.

4. Finally, handle the special case of having a silent e.

4. Optional Extra Challenge: extended Syllable Count

Optional: Your correct solution in the previous problem gives a good estimate of the number of syllables in an English word. However, it is not perfect. As an extra challenge, write a program betterSyllableCount.py that implements additional rules to try and capture a better estimate of the number of syllables in a word.

You're free to implement whatever rules you can think of to get the most accurate answer possible! Here are some suggested extensions:

1. ...and sometimes Y... For several English words, y is treated as a vowel. For example, python has two syllables because y acts as a vowel. Modify your program to account for extra syllables when y acts as a vowel.

2. vowel groups with multiple syllables. Sometimes, the correspondence between vowel groups and syllables doesn't hold. For example, melodious has four syllables, but only three vowel groups. Try to come up with a clever rule to determine when a vowel group corresponds to multiple syllables.

Each lab has 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.
Don't forget 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.