Homework 2: Dictionaries, If Statements, Functions, and Modules#

✅ Put your name here

#

CMSE 201 – Spring 2026#

Learning Goals#

Content Goals#

  • Use if/elif/else statements to implement a logical flow

  • Write and execute functions

  • Use the numpy module for math and data

  • Display data on plots using matplotlib

  • Utilize dictionaries to store data

Practice Goals#

  • Coding conventions

  • More advanced debugging

  • Using functions to build reusable/transferable code

  • Use visualization best practices to make informative plots

Assignment instructions#

Work through the following assignment, making sure to follow all the directions and answer all the questions.

This assignment is due at 11:59 pm on Friday, February 27th. It should be uploaded into the “Homework Assignments” submission folder for Homework #2. Submission instructions can be found at the end of the notebook.

Academic integrity statement (2 Points)#

In the markdown cell below, paste your personal academic integrity statement. By including this statement, you are confirming that you are submitting this as your own work and not that of someone else.

Put your personal academic integrity statement here.#


1. Dictionaries#

Python dictionaries are particularly useful for keeping track of pieces of information of different types.

Scenario: Scenario: You are planning a cross-country road trip. To stay organized, you want to create a travel log that stores both specific details about your destinations and lists of items you need to pack for different weather conditions.

✅  1.1 Task (5 Points)#

In the cell below, create a Python dictionary named trip_plan that includes:

  • 5 entries where the key is a “City Name” and the value is a string describing a “Top Landmark.”

  • 5 entries where the key is a “Weather Type” (e.g., ‘Rainy’, ‘Cold’) and the value is a list of at least 3 clothing items needed for that weather.

# Put your answer here

✅  1.2 Task (5 Points)#

You have made your dictionary/trip_plan, now it’s time to use it!

In the cell below, construct two print statements using entries in your trip_plan dictionary:

  • (1) Print a sentence that mentions a city and retrieves its landmark from the dictionary.;

  • (2) Print two sentences: the first should mention a weather type, and the second should use a specific item retrieved from that weather’s equipment list.

For example, if your city entry is "Paris": "The Eiffel Tower", your output could be:

'I am visiting Paris to see the Eiffel Tower.'

If your weather entry is "Cold": ["Heavy jacket", "Gloves", "Scarf"], your output could be:

The weather today is Cold. I should make sure to pack my Gloves.

# put your answer here

✅  1.3 Metacognition (4 Points)#

  1. How could you add an additional item to this dictionary (without hard-coding it manually)?

  2. What is the advantage of using a dictionary for this task?

Put your answers here.#

2. Debugging a Function#

In the Tasks below, you will be asked to find, describe, and fix the bugs in each example.

✅  2.1 Task (5 Points)#

In the cell below, you will find a function that takes in the weight of a package and returns the shipping cost based on specific weight tiers. However, when you run the test_weights provided, some of the shipping costs for heavier packages seem much too high (defaulting to the most expensive tier incorrectly).

In the cell below, state where the bug is occurring, how you figured it out, and then fix the bug.

# Function to calculate shipping cost based on package weight (lbs)
def shipping_calculator(weight):
    if weight <= 2:
        cost = 5.00
    elif weight <= 5:
        cost = 8.50
    elif weight <= 10:
        cost = 12.00
    elif weight <= 15:
        cost = 15.00
    elif weight == 20: 
        cost = 20.00
    else:
        cost = 50.00 
    return cost
test_weights = [1, 4, 10, 12, 18, 20, 25] # Package weights in lbs

for w in test_weights:
    print("Weight:", w, "lbs -> Cost:", shipping_calculator(w))
Weight: 1 lbs -> Cost: 5.0
Weight: 4 lbs -> Cost: 8.5
Weight: 10 lbs -> Cost: 12.0
Weight: 12 lbs -> Cost: 15.0
Weight: 18 lbs -> Cost: 50.0
Weight: 20 lbs -> Cost: 20.0
Weight: 25 lbs -> Cost: 50.0
## Put your answer here

✅  2.2 Task (5 Points)#

In the cell below, you have a function and a function call written for you, but not quite correctly. You should do/answer the following:

  1. What should the output of the function call be?

  2. What line(s) are causing the bug?

  3. Write a plain English description of the bug.

  4. Fix the bug!

destination = "Iceland"
activity = "hiking"
weather = "chilly"

# A function to describe a vacation spot
# Correct this code!
def vacation_planner(place = "The Beach", action = "sunbathing", temp = "hot"):
    description = "I am going to " + destination + " for " + activity + " and I hope it is " + weather + "."
    return description

# Testing the function with custom values
vacation_planner("Tokyo", "sightseeing", "humid")
'I am going to Iceland for hiking and I hope it is chilly.'

Put your answers here.#

3. Functions, if/else, and Marine Pollution!#

Environmental scientists monitoring ocean health categorize plastic debris based on their size (diameter in millimeters). Three critical categories used in research are:

  • Nanoplastics: \(Size < 0.001\) mm

  • Microplastics: \(0.001 \le Size \le 5.0\) mm

  • Macroplastics: \(Size > 5.0\) mm

3.1 Identifying Microplastics (3 points)#

Write a function called is_microplastic that takes in the size of a piece of debris (in mm) and returns the Boolean True if it falls within the Microplastic range. Otherwise, it should return False.

Task: Write your function in the cell below. Demonstrate that your function works using the provided test data (debris_size_test_data).

# test debris size data (in millimeters)
debris_size_test_data = [0.0005, 2.5, 8.0, 0.0001, 4.8, 15.0, 0.0009, 25.5, 0.5, 1.2]
# [Write your function and test loop here]

3.2 Extending to the Full Data Set (2 points)#

In the cell below, you will find a larger data set of debris sizes. Use your is_microplastic function to count the total number of items in the list that are categorized as Microplastics.

# full debris data set (in millimeters)

debris_sizes = [0.00021, 4.3, 0.8, 0.15, 5.5, 0.002, 3.2, 28.1, 0.0004, 4.8, 1.1, 0.0006, 35.5, 0.0009, 4.2, 5.2, 2.5, 0.00018, 3.2, 1.5, 0.02, 3.9, 4.1, 0.0007, 5.1, 2.4, 0.0013, 0.2, 3.8, 0.00049, 0.0001, 12.4, 0.004, 6.2, 0.9, 1.1, 0.0003, 0.0008, 10.5, 0.001, 0.456, 0.00034, 12.89, 0.0023, 1.12, 6.78, 0.00056, 0.0089, 22.4, 0.9, 4.12, 0.00012, 14.5, 0.0045, 2.34, 8.9, 0.00067, 0.012, 18.2, 1.1, 0.56, 0.00089, 25.6, 0.00023, 3.45, 7.8, 0.00034, 0.056, 32.1, 2.3, 1.2, 0.00011, 11.2, 0.0034, 4.56, 6.7, 0.00078, 0.09, 15.4, 0.8, 0.78, 0.00045, 13.4, 0.0056, 1.23, 9.1, 0.00012, 0.023, 28.9, 1.5, 3.4, 0.00067, 16.7, 0.00089, 2.34, 5.4, 0.00045, 0.078, 42.1, 0.9, 2e-05, 4e-05, 6e-05, 8e-05, 0.0001, 0.00012, 0.00014, 0.00016, 0.00018, 0.0002, 0.00022, 0.00024, 0.00026, 0.00028, 0.0003, 0.00032, 0.00034, 0.00036, 0.00038, 0.0004, 0.00042, 0.00044, 0.00046, 0.00048, 0.0005, 0.00052, 0.00054, 0.00056, 0.00058, 0.0006, 0.00062, 0.00064, 0.00066, 0.00068, 0.0007, 0.00072, 0.00074, 0.00076, 0.00078, 0.0008, 0.00082, 0.00084, 0.00086, 0.00088, 0.0009, 0.00092, 0.00094, 0.00096, 0.00098, 0.001, 0.00102, 0.00104, 0.00106, 0.00108, 0.0011, 0.00112, 0.00114, 0.00116, 0.00118, 0.0012, 0.00122, 0.00124, 0.00126, 0.00128, 0.0013, 0.00132, 0.00134, 0.00136, 0.00138, 0.0014, 0.00142, 0.00144, 0.00146, 0.00148, 0.0015, 0.00152, 0.00154, 0.00156, 0.00158, 0.0016, 0.00162, 0.00164, 0.00166, 0.00168, 0.0017, 0.00172, 0.00174, 0.00176, 0.00178, 0.0018, 0.00182, 0.00184, 0.00186, 0.00188, 0.0019, 0.00192, 0.00194, 0.00196, 0.00198, 0.002, 0.00202, 0.00204, 0.00206, 0.00208, 0.0021, 0.00212, 0.00214, 0.00216, 0.00218, 0.0022, 0.00222, 0.00224, 0.00226, 0.00228, 0.0023, 0.00232, 0.00234, 0.00236, 0.00238, 0.0024, 0.00242, 0.00244, 0.00246, 0.00248, 0.0025, 0.00252, 0.00254, 0.00256, 0.00258, 0.0026, 0.00262, 0.00264, 0.00266, 0.00268, 0.0027, 0.00272, 0.00274, 0.00276, 0.00278, 0.0028, 0.00282, 0.00284, 0.00286, 0.00288, 0.0029, 0.00292, 0.00294, 0.00296, 0.00298, 0.003, 0.00302, 0.00304, 0.00306, 0.00308, 0.0031, 0.00312, 0.00314, 0.00316, 0.00318, 0.0032, 0.00322, 0.00324, 0.00326, 0.00328, 0.0033, 0.00332, 0.00334, 0.00336, 0.00338, 0.0034, 0.00342, 0.00344, 0.00346, 0.00348, 0.0035, 0.00352, 0.00354, 0.00356, 0.00358, 0.0036, 0.00362, 0.00364, 0.00366, 0.00368, 0.0037, 0.00372, 0.00374, 0.00376, 0.00378, 0.0038, 0.00382, 0.00384, 0.00386, 0.00388, 0.0039, 0.00392, 0.00394, 0.00396, 0.00398, 0.004, 0.00402, 0.00404, 0.00406, 0.00408, 0.0041, 0.00412, 0.00414, 0.00416, 0.00418, 0.0042, 0.00422, 0.00424, 0.00426, 0.00428, 0.0043, 0.00432, 0.00434, 0.00436, 0.00438, 0.0044, 0.00442, 0.00444, 0.00446, 0.00448, 0.0045, 0.00452, 0.00454, 0.00456, 0.00458, 0.0046, 0.00462, 0.00464, 0.00466, 0.00468, 0.0047, 0.00472, 0.00474, 0.00476, 0.00478, 0.0048, 0.00482, 0.00484, 0.00486, 0.00488, 0.0049, 0.00492, 0.00494, 0.00496, 0.00498, 0.005, 0.00502, 0.00504, 0.00506, 0.00508, 0.0051, 0.00512, 0.00514, 0.00516, 0.00518, 0.0052, 0.00522, 0.00524, 0.00526, 0.00528, 0.0053, 0.00532, 0.00534, 0.00536, 0.00538, 0.0054, 0.00542, 0.00544, 0.00546, 0.00548, 0.0055, 0.00552, 0.00554, 0.00556, 0.00558, 0.0056, 0.00562, 0.00564, 0.00566, 0.00568, 0.0057, 0.00572, 0.00574, 0.00576, 0.00578, 0.0058, 0.00582, 0.00584, 0.00586, 0.00588, 0.0059, 0.00592, 0.00594, 0.00596, 0.00598, 0.006, 0.008, 0.016, 0.024, 0.032, 0.04, 0.048, 0.056, 0.064, 0.072, 0.08, 0.088, 0.096, 0.104, 0.112, 0.12, 0.128, 0.136, 0.144, 0.152, 0.16, 0.168, 0.176, 0.184, 0.192, 0.2, 0.208, 0.216, 0.224, 0.232, 0.24, 0.248, 0.256, 0.264, 0.272, 0.28, 0.288, 0.296, 0.304, 0.312, 0.32, 0.328, 0.336, 0.344, 0.352, 0.36, 0.368, 0.376, 0.384, 0.392, 0.4, 0.408, 0.416, 0.424, 0.432, 0.44, 0.448, 0.456, 0.464, 0.472, 0.48, 0.488, 0.496, 0.504, 0.512, 0.52, 0.528, 0.536, 0.544, 0.552, 0.56, 0.568, 0.576, 0.584, 0.592, 0.6, 0.608, 0.616, 0.624, 0.632, 0.64, 0.648, 0.656, 0.664, 0.672, 0.68, 0.688, 0.696, 0.704, 0.712, 0.72, 0.728, 0.736, 0.744, 0.752, 0.76, 0.768, 0.776, 0.784, 0.792, 0.8, 0.808, 0.816, 0.824, 0.832, 0.84, 0.848, 0.856, 0.864, 0.872, 0.88, 0.888, 0.896, 0.904, 0.912, 0.92, 0.928, 0.936, 0.944, 0.952, 0.96, 0.968, 0.976, 0.984, 0.992, 1.0, 1.008, 1.016, 1.024, 1.032, 1.04, 1.048, 1.056, 1.064, 1.072, 1.08, 1.088, 1.096, 1.104, 1.112, 1.12, 1.128, 1.136, 1.144, 1.152, 1.16, 1.168, 1.176, 1.184, 1.192, 1.2, 1.208, 1.216, 1.224, 1.232, 1.24, 1.248, 1.256, 1.264, 1.272, 1.28, 1.288, 1.296, 1.304, 1.312, 1.32, 1.328, 1.336, 1.344, 1.352, 1.36, 1.368, 1.376, 1.384, 1.392, 1.4, 1.408, 1.416, 1.424, 1.432, 1.44, 1.448, 1.456, 1.464, 1.472, 1.48, 1.488, 1.496, 1.504, 1.512, 1.52, 1.528, 1.536, 1.544, 1.552, 1.56, 1.568, 1.576, 1.584, 1.592, 1.6, 1.608, 1.616, 1.624, 1.632, 1.64, 1.648, 1.656, 1.664, 1.672, 1.68, 1.688, 1.696, 1.704, 1.712, 1.72, 1.728, 1.736, 1.744, 1.752, 1.76, 1.768, 1.776, 1.784, 1.792, 1.8, 1.808, 1.816, 1.824, 1.832, 1.84, 1.848, 1.856, 1.864, 1.872, 1.88, 1.888, 1.896, 1.904, 1.912, 1.92, 1.928, 1.936, 1.944, 1.952, 1.96, 1.968, 1.976, 1.984, 1.992, 2.0, 2.008, 2.016, 2.024, 2.032, 2.04, 2.048, 2.056, 2.064, 2.072, 2.08, 2.088, 2.096, 2.104, 2.112, 2.12, 2.128, 2.136, 2.144, 2.152, 2.16, 2.168, 2.176, 2.184, 2.192, 2.2, 2.208, 2.216, 2.224, 2.232, 2.24, 2.248, 2.256, 2.264, 2.272, 2.28, 2.288, 2.296, 2.304, 2.312, 2.32, 2.328, 2.336, 2.344, 2.352, 2.36, 2.368, 2.376, 2.384, 2.392, 2.4, 2.408, 2.416, 2.424, 2.432, 2.44, 2.448, 2.456, 2.464, 2.472, 2.48, 2.488, 2.496, 2.504, 2.512, 2.52, 2.528, 2.536, 2.544, 2.552, 2.56, 2.568, 2.576, 2.584, 2.592, 2.6, 2.608, 2.616, 2.624, 2.632, 2.64, 2.648, 2.656, 2.664, 2.672, 2.68, 2.688, 2.696, 2.704, 2.712, 2.72, 2.728, 2.736, 2.744, 2.752, 2.76, 2.768, 2.776, 2.784, 2.792, 2.8, 2.808, 2.816, 2.824, 2.832, 2.84, 2.848, 2.856, 2.864, 2.872, 2.88, 2.888, 2.896, 2.904, 2.912, 2.92, 2.928, 2.936, 2.944, 2.952, 2.96, 2.968, 2.976, 2.984, 2.992, 3.0, 3.008, 3.016, 3.024, 3.032, 3.04, 3.048, 3.056, 3.064, 3.072, 3.08, 3.088, 3.096, 3.104, 3.112, 3.12, 3.128, 3.136, 3.144, 3.152, 3.16, 3.168, 3.176, 3.184, 3.192, 3.2, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5, 24.0, 24.5, 25.0, 25.5, 26.0, 26.5, 27.0, 27.5, 28.0, 28.5, 29.0, 29.5, 30.0, 30.5, 31.0, 31.5, 32.0, 32.5, 33.0, 33.5, 34.0, 34.5, 35.0, 35.5, 36.0, 36.5, 37.0, 37.5, 38.0, 38.5, 39.0, 39.5, 40.0, 40.5, 41.0, 41.5, 42.0, 42.5, 43.0, 43.5, 44.0, 44.5, 45.0, 45.5, 46.0, 46.5, 47.0, 47.5, 48.0, 48.5, 49.0, 49.5, 50.0, 50.5, 51.0, 51.5, 52.0, 52.5, 53.0, 53.5, 54.0, 54.5, 55.0, 55.5, 56.0, 56.5, 57.0, 57.5, 58.0, 58.5, 59.0, 59.5, 60.0, 60.5, 61.0, 61.5, 62.0, 62.5, 63.0, 63.5, 64.0, 64.5, 65.0, 65.5, 66.0, 66.5, 67.0, 67.5, 68.0, 68.5, 69.0, 69.5, 70.0, 70.5, 71.0, 71.5, 72.0, 72.5, 73.0, 73.5, 74.0, 74.5, 75.0, 75.5, 76.0, 76.5, 77.0, 77.5, 78.0, 78.5, 79.0, 79.5, 80.0, 80.5, 81.0, 81.5, 82.0, 82.5, 83.0, 83.5, 84.0, 84.5, 85.0, 85.5, 86.0, 86.5, 87.0, 87.5, 88.0, 88.5, 89.0, 89.5, 90.0, 90.5, 91.0, 91.5, 92.0, 92.5, 93.0, 93.5, 94.0, 94.5, 95.0, 95.5, 96.0, 96.5, 97.0, 97.5, 98.0, 98.5, 99.0, 99.5, 100.0, 100.5, 101.0, 101.5, 102.0, 102.5, 103.0, 103.5, 104.0, 104.5, 105.0, 105.5, 106.0, 106.5, 107.0, 107.5]

3.3 Adding Complexity (3 points)#

What if you wanted to categorize all types of debris at once? Write a new function called categorize_debris that takes in a debris size and returns a string: "Nanoplastic", "Microplastic", or "Macroplastic".

You can choose to build from your previous code or start from scratch. You may find it helpful to use the test data again while debugging!

Task: Demonstrate that your code works by counting the number of each type in the full data set and printing the final counts.

# put your work here

3.4 Metacognition (4 points)#

✅  In the cell below, answer the following reflection questions:

  • In Part 3.3, did you use your function from 3.1 at all?

  • In what way? (Or if you didn’t, why not?)

  • If writing a function was the most efficient way to solve this problem, why? If not, what other way(s) could you solve it?

Put your answers here.#

4. Numpy, matplotlib, and functions#

4.1 Making a plotting function (6 points)#

In CMSE 201, you will be making plots often. To help future you save time, you are going to write a function to use when you make plots!

✅ In the cells below, write a function that takes in:

  • an array of x-values

  • a list of 4 arrays of y-values

  • a list of 4 labels

  • a title

Your function must create a figure containing 4 subplots using plt.subplot().

Each subplot must include:

  • a title

  • x-axis label

  • y-axis label

  • a legend

You should also use different colors and markers for each subplot.

You must test your function with the test data below to receive full credit.

#### Stock Style Test Data

import numpy as np

days = np.arange(1, 11)

stock_A = np.array([100, 101, 102, 101, 103, 104, 103, 105, 106, 107])
stock_B = np.array([50,  49,  50,  51,  52,  51,  53,  54,  55,  56])
stock_C = np.array([200, 198, 199, 201, 202, 203, 205, 204, 206, 208])
stock_D = np.array([75,  76,  74,  73,  74,  75,  76,  78,  79,  80])
# put your answer here

4.2 Using numpy arrays and functions for calculations (6 points)#

Stock prices can vary a lot from day to day, so how can we compare how prices change across different stocks? We will use the percent difference (daily percent change).

We can calculate percent difference like this:

\[ \% \ difference = \frac{(final-initial)}{initial} \times 100 \]

✅ In the cell below, write a function that takes in an array of y-values (stock prices) and calculates the percent difference between each value and the previous value.

Your function should return a new NumPy array of percent differences with length len(y) - 1.

You must test your function using the stock-style test data from 4.1.

# put your code here

4.3 Putting it all together (6 points)#

Along with this homework assignment, you will find a file called stock_data.csv containing daily stock prices for several stocks.

✅ In the cell(s) below, use the data from the file (and the functions you wrote in 4.1 and 4.2) to do the following:

  1. Load the dataset using np.loadtxt()

  2. Choose four stocks from the dataset (for example: Stock_1, Stock_2, Stock_3, Stock_4)

  3. Use your function from 4.2 to compute the daily percent difference for each selected stock

  4. Use your function from 4.1 to create a 4-subplot figure showing the percent differences over time


Important Note#

Because percent differences are computed between consecutive days, your percent difference arrays will have length len(prices) - 1.
So your x-values should be:

x[1:]
# put your work here

4.4 Metacognition (4 points)#

  1. How would you change need to change your function if you added additional stock data sets? Or, if you wouldn’t have to change it, what about your design makes that possible?

  2. Walk us through your problem solving process for 4.1, 4.2, and 4.3. You should include at least one sentence per part and make sure to talk about why you made the choices you did!

Put your answers here.#


Congratulations, you’re done!#

Submit this assignment by uploading it to the course Desire2Learn web page. Go to the “Homework Assignments” section, find the submission folder link for Homework #2, and upload it there.