Hurricane analysis Question 3 troubleshooting

Hi Everyone,

For part 3 of the Hurricane analysis project my answer is roughly the same as in the given solution but when I check the output of the resultant dictionary I only get one record for each year.

# 3
# Organizing by Year
def organize_by_year(hurricanes):
    organized_by_year = {}
    for cane in hurricanes:
        current_year = hurricanes[cane]["Year"]
        current_cane =  hurricanes[cane]
        if current_year not in organized_by_year:
            organized_by_year[current_year] = [current_cane]
        else:
            organized_by_year[current_year].append(current_cane)
    return organized_by_year
# create a new dictionary of hurricanes with year and key
hurricanes_by_year = organize_by_year(hurricanes)
print(hurricanes_by_year[1961])

any advice on how to fix this? thanks

It works, no? I got this output w/your code:

[{'Name': 'Carla', 'Month': 'September', 'Year': 1961, 'Max Sustained Wind': 175, 'Areas Affected': ['Texas', 'Louisiana', 'Midwestern United States'], 'Damage': 326000000.0, 'Deaths': 43}, {'Name': 'Hattie', 'Month': 'October', 'Year': 1961, 'Max Sustained Wind': 160, 'Areas Affected': ['Central America'], 'Damage': 60300000.0, 'Deaths': 319}]


When printing a year with two hurricanes I recieve the same name twice instead of two different names not sure the source of this issue something about how the records are being assigned to organized_by_year

It could be your code prior to that step.
Admittedly, I used my code from the previous steps and then plugged in your function, rather than my own.

What was your code for the previous step? (The function where you created the initial dictionary w/the name, month, years, etc).

here is the code prior to that step:

def create_dictionary(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths):
    """Create dictionary of hurricanes with hurricane name as the key and a dictionary of hurricane data as the value."""
    hurricanes = dict()
    num_hurricanes = len(names)
    for i in range(num_hurricanes):
        hurricanes[names[i]] = {"Name": names[i],
                              "Month": months[i],
                              "Year": years[i],
                              "Max Sustained Wind": max_sustained_winds[i],
                              "Areas Affected": areas_affected[i],
                              "Damage": updated_damages[i],
                              "Deaths": deaths[i]}
    return hurricanes

# create hurricanes dictionary
hurricanes = create_dictionary(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths)

I think I may just redownload the notebook start fresh because I’m not exactly sure yet what the issue is.

Maybe change that :arrow_up:

to this:

oranized_by_year = dict()

?

I went to the beginning of my jupyter notebook and re-ran all the code blocks in order - this resolved the dictionary issue

1 Like

Ah, ha! Cool. Yea, I forgot that they sometimes “time out”…

1 Like