key - a piece of data that can be found in the dictionary easilyvalue - a piece of data that is associated with the keyCommon use cases: store associations
JSON api requests: https://api.weather.gov/gridpoints/MPX/109,72/forecast
len(dict)dict[key] – crash if not founddict.get(key, default) – default if not founddict[key] = value
len(dict) will increasedel dict[key] – crash if not founddict.pop(key, default) – default if not found4 syntax, 3 options
Write a function count_coins(coins) that counts U.S. coins in a list and returns a dictionary mapping coin names to the number of times they appear in the list of integers representing coin values (in cents). If no coins is represented in the argument list, its count should be zero.
The dictionary should use coin names, not values:
Submit your count_coins.py file to gradescope.
Test case:
Ignore values that do not match coin names in U.S. English.
Start with a populated dictionary:
Start with an empty dictionary (will not pass all autograder tests):
def count_coins(coins):
counts = {}
for coin in coins:
if coin == 1:
counts["pennies"] = counts.get("pennies", 0) + 1
elif coin == 5:
counts["nickels"] = counts.get("nickels", 0) + 1
elif coin == 10:
counts["dimes"] = counts.get("dimes", 0) + 1
elif coin == 25:
counts["quarters"] = counts.get("quarters", 0) + 1
return countsStart with an empty dictionary (will not pass all autograder tests):
def count_coins(coins):
counts = {}
for coin in coins:
match coin:
case 1:
counts["pennies"] = counts.get("pennies", 0) + 1
case 5:
counts["nickels"] = counts.get("nickels", 0) + 1
case 10:
counts["dimes"] = counts.get("dimes", 0) + 1
case 25:
counts["quarters"] = counts.get("quarters", 0) + 1
return counts
if __name__ == "__main__":
coins = [1, 25, 1, 25, 1, 10, 5]
counted_coins = count_coins(coins)
assert counted_coins == {"pennies": 3,
"quarters": 2,
"dimes": 1,
"nickels": 1}
print(counted_coins){'pennies': 3, 'quarters': 2, 'dimes': 1, 'nickels': 1}