To start this assignment, download this zip file.
The following guide pages cover material needed for this assignment:
Lab 3c — Lists
Preparation
1 minute
Download the zip file for this lab, located above. This zip file has code that
you will use for this assignment. Extract the files and put them in your cs110
directory in a folder called lab3c
.
Warmup
5 minutes
Consider the following code:
if __name__ == '__main__':
puppies = ['labrador', 'greyhound', 'golden retriever', 'poodle']
puppies.append('australian sheepdog')
puppies = []
puppies.append('goldendoodle')
print(puppies)
(1) What does the code print?
(2) Using the file puppies.py
, step through the code to see what it does.
(3) Discuss with the TA as needed to understand what it does.
Shopping List
10 minutes
In shopping_list.py
, you will find a program that is supposed to let you enter
a list of items for a grocery list. You finish entering items by entering a
blank line (just pressing enter). But this is what it does if you run it:
Enter an item: milk
Enter an item: eggs
Enter an item: butter
Enter an item:
['']
(1) Working with a partner, use the debugger to step through the code and see what is going wrong.
(2) With your partner, fix the code so it works properly.
(3) Discuss what bugs you found and how you fixed them.
Places Visited
15 minutes
Use the file places_visited.py
to write a program that asks you to enter
places you have visited, then places you want to visit, and then prints those out.
You finish entering places by entering a blank line (just pressing enter).
The output should look like this:
Enter a place you have visited: Oregon
Enter a place you have visited: Washington, D.C.
Enter a place you have visited: Italy
Enter a place you have visited:
Enter a place you want to visit: Maine
Enter a place you want to visit: Grand Canyon
Enter a place you want to visit: Hawaii
Enter a place you want to visit:
I have visited:
- Oregon
- Washington, D.C.
- Italy
I want to visit:
- Maine
- Grand Canyon
- Hawaii
Notice how the code you would write to get the first and second lists are nearly identical.
In fact, they are the same except for the prompt given to the input
function.
Notice also how both lists are printed out with the same format, just with different headers. We can follow the same principle as we did for getting the lists of places and write a single function that can print any list of items.
The starter code looks like this:
def get_list(prompt):
responses = []
while True:
response = input(prompt)
if response == '':
break
responses.append(response)
return responses
def print_list(items):
for item in items:
print(f'- {item}')
def main():
# Write code here
pass
if __name__ == '__main__':
main()
Use the provided get_list
and print_list
functions to implement this activity.
Functions like get_list
and print_list
, that can be easily customized for a variety of contexts through a parameter,
are called generic functions.
Writing and using generic functions is an important skill in programming.
Whenever you see that your code needs to do essentially the same thing more than once,
consider writing a generic function with a parameter that controls the difference
(for example, the prompt used by input
in the get_list
function above).
Discuss how you would use get_list
and print_list
with a TA, and then write your code.
Puppy Libs
15 minutes
Use the file puppy_libs.py
to write a program that asks you to enter a series
of adjectives followed by a series of verbs ending in ing
.
Both lists will end with a blank line.
The program then prints out The puppy is {word}!
for each adjective and then each verb.
It ends by printing The puppy is taking a nap.
Here is some sample input and output:
Enter an adjective: soft
Enter an adjective: cute
Enter an adjective: drooly
Enter an adjective:
Enter a verb ending in "ing": playing
Enter a verb ending in "ing": jumping
Enter a verb ending in "ing": barking
Enter a verb ending in "ing": eating
Enter a verb ending in "ing": laughing
Enter a verb ending in "ing":
The puppy is soft!
The puppy is cute!
The puppy is drooly!
The puppy is playing!
The puppy is jumping!
The puppy is barking!
The puppy is eating!
The puppy is laughing!
The puppy is taking a nap.
The starter code looks like this:
def main():
# Write code here
pass
if __name__ == '__main__':
main()
Start by making a list of the functions you will call inside of main()
to
decompose the problem. Be sure to write a generic function for getting each list of words:
Function | Parameters | Description |
---|---|---|
1. | ||
2. | ||
Discuss the functions you would use with the TA, and then write your code.
Afer you write the code, discuss with the TA:
- What does this program have in common with the places visited one?
Grading
To finish this lab and receive a grade, take the canvas quiz.
We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊