COS 100: Introduction to Programming
Interim 2022
Lab 4: Repeating
In-class practice labs will not directly impact your grade (although you'll get your standard participation point for staying on task), and will serve to help you with homework projects.Lab 4A: I went to Buffalo and all I got was this lousy T-shirt
- Background: Due to a weird quirk of the English language, the word "buffalo" can be a noun (the animal, singular and plural), a verb (to baffle or confuse), or the city (in New York). Because of this, repeating the word "buffalo" any number of times is still a legal sentence in English. This example is commonly used to demonstrate the difficulty of getting a computer to parse natural languages. (Most humans would assume something's wrong with you if you're repeating the word "buffalo" over and over, but a computer thinks you're saying something legitimate. The computer is technically correct.)
- Task: Prompt the user for a sentence length, and then use a loop to print out a sentence consisting of the requested number of "buffalo"s. Start the sentence with a capital letter, and end it with a period.
-
Suggested order of development:
- Print the right number of "buffalo"s, all lowercase, one per line.
- Make them print on the same line, separated by spaces.
- Capitalize the first letter and add a period at the end.
-
Sample run:
How many words do you want? 6 Buffalo buffalo buffalo buffalo buffalo buffalo.
Lab 4B: There is a better way to do that
- Go back and do Lab 3C as follows:
- First try to do it without loops. You don't have to finish it fully, but do enough that you see the pattern and are convinced that you can finish it easily (with grunt work/copy-and-paste).
- Then, use loops to simplify your code and remove redundancies.
- Skipping Step 1 (without loops) and trying Step 2 (use loops) immediately will often lead to failures and frustrations.
- Getting
1st
,2nd
, and3rd
to appear in the prompts will be challenging, and that's not the main point of this lab. So here's a suggested order of development:- Use
Please enter number:
as prompt. Get the program to work properly reporting the average. - Use
Please enter your # number:
as prompt, where#
is1
,2
,3
, etc. - (Challenge) Use
Please enter your ### number:
as prompt, where###
is1st
,2nd
,3rd
, etc.
- Use
Challenge Lab 4C: This is repetitive and redundant and repetitive and redundant
- Modify Lab 4A so the program keeps asking for number of words. The user can say "0" to quit.
-
Sample single run:
How many words do you want? 3 Buffalo buffalo buffalo. How many words do you want? 1 Buffalo. How many words do you want? 4 Buffalo buffalo buffalo buffalo. How many words do you want? 0 Fine, be that way.