CPSC120A
Fundamentals of Computer Science I

Post-lab 25

Dictionary Traversal

Dictionaries are great because they allow us to reference items with non-numeric keys. However, that causes a problem because we cannot use our simple version of a for loop to iterate over what is stored in the dictionary. However, we have methods that give us particular elements from the dictionary as lists. Demonstrate your knowledge of dictionary methods by writing a function that counts the values in a dictionary.

Details

Write a function called count_values(a_dictionary, a_value). It takes a dictionary and some arbitrary data type that may or may not be in the dictionary. It should return an integer, the number of times the specified value occurs in the dictionary. The function should not use the list count method.

Make sure your program handles all necessary cases gracefully. What additional test cases should you check?

Sample Test Cases

Function Parameters Sample Output
{"a": 1, "b": 2, "c": 2}, 2 2
{"a": 1, "b": 2}, 2 1

Submission

Submit your program as a .py file on the course Inquire page before class on Monday November 10th.