Hurricane Analysis Step 5

Hi,
I’m stuck on step 5 of the Hurricane Analysis Project (where you have to count the occurrence of each area affected). I am trying to use a defaultdict object but keep receiving a ‘NameError’. Please help!

You’d need to import that class first before you use it. It’s part of the collections module-

from collections import defaultdict
4 Likes

Hi! I’m trying to work with the same collection and I’m having an unhashable type: ‘list’ error, here is my piece of code
Thanks! :upside_down_face:

write your count affected areas function here:

from collections import defaultdict

counted_areas = defaultdict(int)
for area in areas_affected:
counted_areas[area] +=1
print(counted_areas)

1 Like

Dictionary keys must be hashable and you’ve used an unhashable type (a list according to the error). Reconsider what you want your dictionary key to be and double check what you’re actually passing to it.

1 Like

halo, I am struggle in this part, and look at some people finished code.
could you help me to understand this code, which part said it has to return Central America : 9.
Thank you

count_affected_area = {‘Central America’: 9, ‘Mexico’: 7, ‘Cuba’: 6, ‘Florida’: 6, ‘The Bahamas’: 7, ‘Lesser Antilles’: 4, ‘United States East Coast’: 3, ‘Atlantic Canada’: 3, ‘Northeastern United States’: 2, ‘Jamaica’: 4, ‘Cayman Islands’: 1, ‘Bermuda’: 2, ‘Texas’: 4, ‘Tamaulipas’: 1, ‘Yucatn Peninsula’: 3, ‘Georgia’: 1, ‘The Carolinas’: 1, ‘Virginia’: 1, ‘Southeastern United States’: 1, ‘Southwestern Quebec’: 1, ‘New England’: 1, ‘Louisiana’: 1, ‘Midwestern United States’: 1, ‘The Caribbean’: 8, ‘United States Gulf Coast’: 6, ‘United States East coast’: 1, ‘South Texas’: 1, ‘Venezuela’: 3, ‘Hispaniola’: 1, ‘South Florida’: 1, ‘Greater Antilles’: 2, ‘Bahamas’: 2, ‘Eastern United States’: 1, ‘Ontario’: 1, ‘Windward Islands’: 1, ‘Nicaragua’: 1, ‘Honduras’: 1, ‘Antilles’: 1, ‘Colombia’: 1, ‘Cape Verde’: 1, ‘British Virgin Islands’: 1, ‘U.S. Virgin Islands’: 1, ‘Virgin Islands’: 1, ‘Puerto Rico’: 1, ‘Dominican Republic’: 1, ‘Turks and Caicos Islands’: 1, ‘United States Gulf Coast (especially Florida Panhandle)’: 1}

def hurricanes_most_affected_area(count_affected_area):
    most_area = ''
    max_count = 0

    for max_area in count_affected_area:
        if count_affected_area[max_area] > max_count:
            max_count = count_affected_area[max_area]
            most_area = max_area

    return most_area, max_count

most_affected_area = hurricanes_most_affected_area(count_affected_area)
print(most_affected_area)
result
'Central America', 9)