Lecture 17 - Exercises

17A - Dictionaries

Write the output of the following program. If a line causes an error, write ERROR at that point in the output and continue on with the program as if the line causing the error had been skipped.

phone_book = {"Wehrwein": 6324}
phone_book["Ahmed"] = 2624
phone_book["Jagodzinski"] = 6160
phone_book["Fizzano"] = 3807
phone_book["Wehrwein"] = 6327

print(phone_book["Wehrwein"])
print(phone_book["Fizzano"])
print(phone_book["Liu"])
print(phone_book.get("Wehrwein"))
print(phone_book.get("Liu", 9999))
print("6327" in phone_book)
print("Wehrwein" in phone_book)
print("Wehrwein" in phone_book.items())

del phone_book["Jagodzinski"]

contents = []
for k in phone_book.keys():
    contents.extend([k, phone_book[k]])
print(len(contents))

print(contents[-1])

Problems

  1. Implement the following function:

  2. Implement the following function; you may call the function you wrote for Problem 1.

  3. Implement the following function:

  4. Implement the following function: