BYU logo Computer Science

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).

This is the expected output:

Enter an item: milk
Enter an item: eggs
Enter an item: butter
Enter an item:
['milk', 'eggs', 'butter']

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 destination you want to visit: Maine
Enter a destination you want to visit: Grand Canyon
Enter a destination you want to visit: Hawaii
Enter a destination 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 and bullets. We can follow the same principle as we did for getting the lists of places.

We provide the print_list function, which takes in three arguments: the header, list, and bullet point. This means it can print any list of items, with any header or bullet point.

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(header, items, bullet):
    print(header)
    for item in items:
        print(f'{bullet} {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.

Furniture Shopping

15 minutes

Use the file furniture_shopping.py to write a program that helps the user know what furniture they can afford. This program already prompts the user for their budget. Complete the main function by correctly using the get_numbers function to prompt the user for the prices of tables and chair sets. They complete the print_smaller function which should a return a list of numbers that are within the users budget.

Here is some sample input and output:

Let's find you some tables and chairs!
First, what is your total budget? 100

Sweet! Now let me know what your table options are.
Enter the price of a table: 20
Enter the price of a table: 10
Enter the price of a table: 50
Enter the price of a table: 150
Enter the price of a table: 

Next, let me know what your chair options are.
Enter the price of a set of chairs: 30
Enter the price of a set of chairs: 50
Enter the price of a set of chairs: 110
Enter the price of a set of chairs: 200
Enter the price of a set of chairs: 

Since your total budget is $100, you can afford the tables that cost: 
- $20
- $10
- $50
Which table do you want? 20

That means that your chair budget is $80.
You can afford chairs that cost: 
- $30
- $50

The starter code looks like this:

def get_numbers(prompt):
    responses = []
    while True:
        response = input(prompt)
        if response == '':
            break
        responses.append(int(response))
    return responses


def print_smaller(numbers, boundary):
    # This function should print the numbers that are smaller than the boundary
    pass


def main():
    print("Let's find you some tables and chairs!")
    total_budget = int(input("First, what is your total budget? "))

    print()
    print("Sweet! Now let me know what your table options are.")
    # 1. Get table options

    print()
    print("Next, let me know what your chair options are.")
    # 2. Get chair options

    print()
    print(f"Since your total budget is ${total_budget}, you can afford the tables that cost: ")
    # 3. Print table options

    table_cost = int(input("Which table do you want? "))
    chair_budget = total_budget - table_cost

    print()
    print(f"That means that your chair budget is ${chair_budget}.")
    print("You can afford chairs that cost: ")
    # 4. Print chair options


if __name__ == "__main__":
    main()

After you write the code, discuss with the TA:

  • What does this program have in common with the places_visited.py?

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. 😊