# Hurricane Analysis Challenge Project (Python)

Hi, here is my code.
Any suggestions, I am never sure if this are best solutions or there is any better “Pythonic” way. Thanks

What did people do this project in? Like what’s recommended? My laptop screen diagonal is only 12" lol so I’m struggling to do it on Codecademy. I’m trying to use Spyder but not finding it great. What did other people use?

my solution. great exercise

A little upgraded solution. One scaling function can process both deaths and damage for to rate hurricanes (starts at line 135). Also it is possible to update scale dictionaries and it will still work

Wanted to add in the link to my solution (I made a github repo where I will be adding the solutions to all the challenges on this data science track), I have changed a couple of things and used some list comprehensions to make it tidier, left some comments in there too to explain my logic. In some cases, I used the actual list in the definition instead of using the hurricane dictionary for information since these are global functions.

My first post on Codecademy forums and first share using GitHub, so let’s see if I mess something up on either end
Hurricane Analysis

This one was really hard. It took me almost a week to complete.
But… here it is. FInally:

Hurricaine Analysis
(Yes, i do realize my spelling is… not cnsistent on the “Hurrcane” word)

I assume that the project does not need the use of Pandas (and DataFrames) and numpy stuff,
although that seems like it would be an interesting way to approach this too.

Here it is my completed project!

Huuricane Analysis

Here is my solution:

I just have a quiestion about how to sort the damages list?
I had to convert the “Damages not registered” to zeros, but is there another more realistic solution?

BTW this project took me a while, about a week to solve it completely. But it was worth it!

Here is my solution! Have a looksee. I completely forgot about the .find() function but other than that I like what I made. Comments welcome

For the Hurricane Analysis Challenge Project

Here’s a version of the solution to just part #3 using stuff from the Intermediate Python course.
I think that it’s complete overkill for this project, but I did it anyway.
And I used a dict comprehension in there too.

``````import inspect

# from stackoverflow.com:
# function to get the name(s) of a variable or value
def retrieve_var(val, use_locals=False):
if use_locals:
callers_local_vars = inspect.currentframe().f_back.f_locals.items()
else:
callers_local_vars = inspect.currentframe().f_back.f_globals.items()
names = [var_name for var_name, var_val in callers_local_vars if var_val is val]
if len(names) == 1:
return names[0]
return names

# args is lists
def make_dict_from_lists(*args, keysource = None ):
list_names = list(map(retrieve_var, args))

def fix_name(text):  # function defined inside a function
result = text.replace('_', ' ').title().strip()
if result[-1] == 's':
return result[:-1]
else:
return result

keys = list(map(fix_name, list_names))
pairs = list(zip(keys, args))

if keysource is None:
keysource = args[0] #previously args[-1]
dictionary = dict()
length = len(keysource)
for i in range(length):
dictionary[keysource[i]] = \
{ key: list[i] for key, list in pairs }
return dictionary

hurricane_dict = make_dict_from_lists(names, months, years, max_sustained_winds, areas_affected, deaths)

print(hurricane_dict['Cuba I'], '\n')
print(hurricane_dict['Ivan'], '\n')
``````

Note that this is not actually what I used in my project; I did something simpler there.

I’m curious if anyone did something similar to the stuff here

Very useful exercise. Here’s my code:

Heres my code… everything works, i think
(https://github.com/SBSCaster/hurricane_analysis/blob/main/script.py)

Super cool project. I learned so much, despite it taking forever. Haha.

Great exercise!

Hurricane Analysis

Hey guys,

I’m stuck on the 5th step (create a dictionary to count areas) of this project challenge, where I keep having a “TypeError: unhashable type: list”.

My code is as follow :

``````# Counting Damaged Areas
def count_area_dictionary(hurricanes):
#create separate dictionnary for area
hurricanes_by_area = {}
for cane in hurricanes:
for area in hurricanes[names]["Areas Affected"]:
if area not in hurricanes_by_area:
hurricanes_by_area[area] = 1
if area in hurricanes_by_area:
huricanes_by_area[area] += 1
return hurricanes_by_area

h_b_areas = count_area_dictionary(hurricanes)
print(h_b_area)
``````

And I’ve seen on the solution.py file that the solution is taking this way. I don’t see how I can go further.

How can I fix this ?
Thanks!

import pandas as pd import numpy as np # names of hurricanes names = ['Cuba I', 'San Felipe II Okeechobee', 'Bahamas', 'Cuba II', 'CubaBrownsville', 'Tampico', 'Labor Day', 'New England', 'Carol', 'Janet', 'Carla', 'Hattie', 'Beulah', 'Camille', 'Edith', 'Anita', 'David', 'Allen', 'Gilbert', 'Hugo', 'Andrew', 'Mitch', 'Isabel', 'Ivan', 'Emily', 'Katrina', 'Rita', 'Wilma', 'Dean', 'Felix', 'Matthew', 'Irma', 'Maria', 'Michael'] # months of hurricanes months = ['October', 'September', 'September', 'November', 'August', 'September', 'September', 'September', 'September', 'September', 'September', 'October', 'September', 'August', 'September', 'September', 'August', 'August', 'September', 'September', 'August', 'October', 'September', 'September', 'July', 'August', 'September', 'October', 'August', 'September', 'October', 'September', 'September', 'October'] # years of hurricanes years = [1924, 1928, 1932, 1932, 1933, 1933, 1935, 1938, 1953, 1955, 1961, 1961, 1967, 1969, 1971, 1977, 1979, 1980, 1988, 1989, 1992, 1998, 2003, 2004, 2005, 2005, 2005, 2005, 2007, 2007, 2016, 2017, 2017, 2018] # maximum sustained winds (mph) of hurricanes max_sustained_winds = [165, 160, 160, 175, 160, 160, 185, 160, 160, 175, 175, 160, 160, 175, 160, 175, 175, 190, 185, 160, 175, 180, 165, 165, 160, 175, 180, 185, 175, 175, 165, 180, 175, 160] # areas affected by each hurricane areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], ['Jamaica', 'Yucatn Peninsula'], ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'], ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'], ['Bermuda', 'New England', 'Atlantic Canada'], ['Lesser Antilles', 'Central America'], ['Texas', 'Louisiana', 'Midwestern United States'], ['Central America'], ['The Caribbean', 'Mexico', 'Texas'], ['Cuba', 'United States Gulf Coast'], ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], ['Mexico'], ['The Caribbean', 'United States East coast'], ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'], ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'], ['The Caribbean', 'United States East Coast'], ['The Bahamas', 'Florida', 'United States Gulf Coast'], ['Central America', 'Yucatn Peninsula', 'South Florida'], ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'], ['The Caribbean', 'Venezuela', 'United States Gulf Coast'], ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], ['Bahamas', 'United States Gulf Coast'], ['Cuba', 'United States Gulf Coast'], ['Greater Antilles', 'Central America', 'Florida'], ['The Caribbean', 'Central America'], ['Nicaragua', 'Honduras'], ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'], ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'], ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic', 'Turks and Caicos Islands'], ['Central America', 'United States Gulf Coast (especially Florida Panhandle)']] # damages (USD(\$)) of hurricanes damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M', '27.9M', '5M', 'Damages not recorded', '306M', '2M', '65.8M', '326M', '60.3M', '208M', '1.42B', '25.4M', 'Damages not recorded', '1.54B', '1.24B', '7.1B', '10B', '26.5B', '6.2B', '5.37B', '23.3B', '1.01B', '125B', '12B', '29.4B', '1.76B', '720M', '15.1B', '64.8B', '91.6B', '25.1B'] # deaths for each hurricane deaths = [90,4000,16,3103,179,184,408,682,5,1023,43,319,688,259,37,11,2068,269,318,107,65,19325,51,124,17,1836,125,87,45,133,603,138,3057,74] #converting damages list def convert_damages_data(damages): """Convert damages data from string to float and return converted data as a list.""" conversion = {"M": 1000000, "B": 1000000000} updated_damages =[] for damage in damages: if damage.find('M') != -1: updated_damages.append(float(damage[0:damage.find('M')])*conversion["M"]) elif damage.find('B') != -1: updated_damages.append(float(damage[0:damage.find('B')])*conversion["B"]) else: updated_damages.append(damage) return updated_damages # update damages data updated_damages = convert_damages_data(damages) # 2 # Create a Table def create_dictionary(names, months, years, areas_affected, max_sustained_winds, deaths, updated_damages): hurricanes={} num_of_hurricanes=len(names) for i in range(num_of_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], "Damages": updated_damages[i], "Deaths":deaths[i]} return hurricanes # Create and view the hurricanes dictionary hurricanes = create_dictionary(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths) # 3 # Organizing by Year def create_year_directory(hurricanes): hurricanes_by_year={} for cane in hurricanes: current_year=hurricanes[cane]["Year"] current_cane=hurricanes[cane] if current_year not in hurricanes_by_year: hurricanes_by_year[current_year]=[current_cane] else: hurricanes_by_year[current_year].append(current_cane) return hurricanes_by_year # create a new dictionary of hurricanes with year and key hurricanes_by_year=create_year_directory(hurricanes) print(hurricanes_by_year[1932]) # 4 # Counting Damaged Areas def count_affected_areas(hurricanes): affected_areas_count={} for cane in hurricanes: for area in hurricanes[cane]["Areas Affected"]: if area not in affected_areas_count: affected_areas_count[area]=1 else: affected_areas_count[area]+=1 return affected_areas_count # create dictionary of areas to store the number of hurricanes involved in affected_areas_count=count_affected_areas(hurricanes)

Following, getting the same error

1 Like

found my error, I had mixed up order of items in dictionary when creating the hurricanes dictionary

check your code you called for cane in hurricanes and in the next line you call for area in hurricanes[name]. that bracket should be cane or whatever you called the element of volume in previous line in for loop

3 Likes