A201 Introduction to Programming 1

A201 Homework 11

Due Date: Tuesday, November 21, 2023.

Goal: to use the dictionary data structure in Python.

Make sure to complete Lab 11 before doing the homework.

Create a Python script file called hw11.py. Add your name at the top as a comment, along with the class name and date.

Ex. 1. a. Grocery List

Write a function called computeTotal(prices, shoppingList) that takes two parameters. The first one is a dictionary containing items found in a shop together with a price for each of them. The second parameter is a shopping list containing items to buy, which is a simple list. The function should return the sum of the prices for all the items in the shopping list.

For example, if the prices are the dictionary:
{'banana': 2, 'apple': 0.8, 'bread': 2.90, 'sugar': 3.5}

and the shopping list is
['apple', 'bread', 'apple']

then the function should return 0.8 + 2.90 + 0.8 = 4.5.

For this, declare a variable total equal to 0. Then use a for loop that goes over the shopping list, and for each element, it adds to the sum the value associated with it in the prices dictionary (its price). Then return the sum at the end.

b. Add a main function where you create a dictionary containing at least 5 items and store it in a variable. Then create a list of items as a shopping list and store it in another variable. Then call the function computeTotal with these two variables and print out the result with an appropriate message.

Add a call to the main below it and test the program to make sure it works.

Ex. 2 Letter Grade

Write a function letterGrade(grades) that inputs a list of homework grades given as percentages (90 for 90%) and returns a letter grade. For this, create a dictionary that contains all the letter grades in descending order with the minimum percentage needed to get that grade. For example, the dictionary would start like this:
{'A': 90, 'B': 80, ... 'F': 0}

Then the function should average the numbers in the list. Then you should use a for loop going over the dictionary of grades, and when you find a key for which the value is less than or equal to the average, return the key.

For example, if the grades are given as the list
[80, 75, 90, 95]
then the average is 85, which should result in an answer of 'B'.

Add a test to this function in the main and then test the program again to see if it works.

Homework Submission

Upload the files lab11.py and hw11.py in Canvas, Assignments, Homework 11.