Python Hurricane Challenge Project

Hi eveyone, I am having some issues with the Hurricane Challenge project.

list_of_info = zip(names, months, years, max_sustained_winds, areas_affected, deaths)

hurricane_info_name = []

for n, m, y, msw, aa, d in list_of_info:

  hurricane = {n: {"Name": n, "Month": m, "Years": y, "Max Sustained Winds": msw, "Areas Affected": aa, "Deaths": d}}

  hurricane_info_name.append(hurricane)

-------------------------------------------------------------------------------------------------------

#*This section does not run*

def death_scale(hurricane_info_name):

  mortality_scale = {0: 0,

                     1: 100,

                     2: 500,

                     3: 1000,

                     4: 10000}

  hurricanes_by_mortality ={0:[], 1:[], 2:[], 3:[], 4:[], 5:[]}

  for cane in hurricane_info_name:

    num_deaths = hurricane_info_name([cane]["Deaths"])

    if num_deaths < mortality_scale[1]: 

      hurricanes_by_mortality[0].append(hurricanes[cane])

  return hurricanes_by_mortality

print(death_scale(hurricane_info_name))

The third section return the error:
Traceback (most recent call last):
File “script.py”, line 167, in
print(death_scale(hurricane_info_name))
File “script.py”, line 160, in death_scale
num_deaths = hurricane_info_name([cane][“Deaths”])
TypeError: list indices must be integers or slices, not str

I have tried multiple different codes, and tried adapting other peoples but ya know how that goes.

Any pointers would help.

hurricane_info_name seems to be a list of dictionaries
so when you do
for cane in hurricane_info_name:
each cane is a dictionary.

so the next line
num_deaths = hurricane_info_name([cane]["Deaths"])
should be
num_deaths = cane["Deaths"]
or
num_deaths = list(cane.values())[0]["Deaths"]

You shouldn’t use the ( ) because its’s a dictionary or list, not a function
and you shouldn’t use hurricane_info[cane] because cane is not the index (its what is stored at each index of hurricane_info).

2 Likes

After running the recommended code above, I received a KeyError. I ran a basic if/else statement and apparently there isn’t a “Death” key in hurricane_info_name or a second dict I made that is not a nested dictionary (to try and simplify my life). Any suggestions?

the code before that has:

hurricane = {n: {"Name": n, "Month": m, "Years": y, "Max Sustained Winds": msw, "Areas Affected": aa, "Deaths": d}}

so you need access to the value for Deaths for a dictionary inside a dictionary …
so there’s an outer dictionary, and you need the first value of that outer dictionary, which is an inner dictionary;
and you need the inner dictionary’s value for the key "Deaths"

Here’s one possibility:

num_deaths = list(cane.values())[0]["Deaths"]

Similarly, inside the if-statement just after this code, you may want
hurricanes_by_mortality[0].append(cane)
or
hurricanes_by_mortality[0].append(list(cane.values())[0])