Chapter 12: Python Dictionaries¶
The answers on this page show ONE way to solve the exercises. However, there are usually OTHER ways to accomplish the same thing. This is OK!
A suggested solution is ONE way to solve the problem, not the ONLY way.
Chapter Sandbox¶
Use the editor below to test out any of the solutions!
Solutions¶
Part A: Search a Dictionary¶
The flavors
dictionary contains entries that pair different ice cream
flavors with their cost per scoop. Your job is to do the following:
Write a function called
return_cost
that takes a dictionary and flavor choice as parameters.The function searches the dictionary for the flavor and returns its cost.
If the flavor is not in the dictionary, return a value of
0
.
1 2 3 4
def return_cost(menu, item): if item in menu: # Check if 'item' is a key in the 'menu' dictionary. return menu[item] # Return the value for the key. return 0 # If 'item' isn't in the dictionary, return 0.
Write a function called
fanciest_flavor
that takes a dictionary as a parameter. The function should return the key name for the most expensive choice in the dictionary.1 2 3 4 5 6 7 8 9 10
# Write your fanciest_flavor function here: def fanciest_flavor(menu): # 'menu' is the dictionary sent to the function. highest_cost = 0 fanciest = '' # Loop through each key/value pair in the dictionary. for (flavor, price) in menu.items(): if price > highest_cost: # Compare the price for the current flavor vs. highest_cost. fanciest = flavor # If True, reassign the most expensive flavor highest_cost = price # and the highest_cost. return fanciest
Part B: Keys from a Collection¶
Use the accumulator pattern to add new key/value pairs to an empty dictionary.
The function should take each name from the list and use it to create a key in the dictionary.
For the value of each key, assign a random integer from 100-500.
1 2 3
tickets = {} # Create an empty dictionary. for name in names: # Loop through the 'names' list. tickets[name] = random.randint(100, 500) # Assign a random number from 100 - 500 to the 'name' key.
In
main()
, call theassign_tickets
function and assign the result to aticket_holders
variable.1 2 3 4 5
def main(): names = ['Caleb', 'Naomi', 'Owen', 'Ava', 'Aaron', 'Lydia'] ticket_holders = assign_tickets(names) print(ticket_holders)
Part C: Modify Values¶
Oh no! Ticket numbers 100-199 were supposed to be held back for VIPs. You need to reassign tickets to anyone who was given one of the reserved seats. Use the editor in part B as you update your code.
The fix_tickets
function should:
Accept a dictionary as a parameter.
Loop through the dictionary and check each ticket number to see if it is in the range 100-199 (including the end points).
For a ticket within the range, increase its value by
500
and reassign it to the key.1 2 3 4 5
def fix_tickets(tickets): # Iterate through the key/value pairs in the dictionary: for (key, value) in tickets.items(): if 100 <= value <= 199: # Check if 'value' is in the desired range. tickets[key] += 500 # If True, increase 'value' by 500 and reassign it to the key.
To check your code, be sure to print
ticket_holders
before and after calling thefix_tickets
function.1 2 3 4
# In main: print('Before:', ticket_holders) fix_tickets(ticket_holders) print('After:', ticket_holders)
Part D: Counting Characters¶
Write a function called character_count
that counts how many times each
character appears in a string.
The function should:
Create an empty dictionary called
counts
.1
counts = {} # Define an empty dictionary.
Loop through the string and check each character.
If the character does NOT exist in as a key in
counts
, add it and assign it a value of1
.If the character DOES exist as a key in
counts
, increase its value by one.
The counting should be case-insensitive. For example,
'a'
and'A'
both count as the same letter.1
for char in a_string.lower(): # Loop through each character in a_string.
Return the completed
counts
dictionary and assign it to aresults
variable inmain()
.1 2 3 4
#In the main function under the variable text # Call the character_count() function. Assign the returned dictionary to results. results = character_count(text)
Part E: Use a List to Sort Key/Value Output¶
Now display the character count result in a cleaner way. Update your code in the part D editor.
Modify your code to print a key/value pair ONLY IF the character is a letter.
1 2 3 4 5 6 7 8 9
# Print the introductory sentence: print("The letter counts for '{0}' are:".format(text)) # Loop through the dictionary: for (key, value) in results.items(): # Check if key is a letter. if key in string.ascii_letters: # If True, print the key and its value from the results dictionary. print(f"{key}: {value}")
Modify your code again, but this time display the character counts in alphabetical order.
Use the
list
function to create a list of the keys from theresults
dictionary.
1 2
# Create a list of the keys from the results dictionary. keys = list(results.keys())
Sort the list, then use a loop to print the key/value pairs, one pair per line.
1 2 3 4 5 6 7 8
# Sort the keys alphabetically. keys.sort() for key in keys: # Check if key is a letter. if key in string.ascii_letters: # If True, print the key and its value from the results dictionary. print('{0}: {1}'.format(key, results[key]))