CPSC120A
Fundamentals of Computer Science I

Post-lab 24

Dictionaries

Dictionaries will cause exceptions if you attempt to access a key that is not currently stored in the dictionary. Often times in our code, however, we might not know what keys are stored where in the dictionary. Demonstrate your knowledge of dictionaries by writing a function to alleviate these concerns.

Details

Write a function called default_get(a_dictionary, key). If key exists in a_dictionary, you should return the values stored for key. If key does not exist in a_dictionary, a \(-1\) should be returned. The function should not use the dictionary get 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}, "a" 1
{"a": 1, "b": 2}, "c" -1

Submission

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