Hurricane Challenge Project - Stuck

I am struggling on the section regarding the hurricane with the highest damage. I keep getting

TypeError: '>' not supported between instances of 'str' and 'float'.

I have gone back through my conversion code and this code multiple times and I am just not seeing where my error is. Does anybody have any suggestions?

def most_expensive_hurricane(hurricanes):
    most_expensive_hurricane = ''
    damage_cost = 0
    for i in hurricanes:
        if hurricanes[i]['Damage'] == 'Damages not recorded':
        if hurricanes[i]['Damage'] > damage_cost:
            most_expensive_hurricane = i
            damage_cost = hurricanes[i]['Damage']
    return most_expensive_hurricane, damage_cost

Here is the code for the conversion. I went back and basically updated it to reflect the solution to ensure it was working correctly but I’m STILL getting that error. :sob:

def damages_conversion(damages): 
    conversion = {"M": 1000000,
               "B": 1000000000}
    updated_damages = list()
    for i in damages: 
        if i == 'Damages not recorded': 
        elif i.find('M') != -1: 
        elif i.find('B') != -1: 
    return updated_damages

Any suggestions/advice would be greatly appreciated.

Is the data in that location string or numeric?

If this is causing you an error because they’re not both numbers …

if hurricanes[i]['Damage'] > damage_cost:

could be changed to

damage = damages_conversion([ hurricanes[i]['Damage'] ])[0]
if not isinstance(damage, (float, int)):
  damage = 0
if damage > damage_cost:

although I think a better approach would be to change the for-loop to iterate through something that’s already a dictionary or list that has the damages as floats or integers already.

I think it’s mixed. The initial part of the project wanted you to convert some strings to floats, but anything that didn’t have numbers retained the “Damages not recorded” value. I am certain this is where the problem is stemming from but I’m at a loss on how to fix it. I can try to upload the entire project if that would help.

The first part of the project asked to convert this data to float but retain the missing data as “Damages not recorded”

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']

With my code I was able to convert it to the following:

['Damages not recorded', 100000000.0, 'Damages not recorded', 40000000.0, 27900000.0, 5000000.0, 'Damages not recorded', 306000000.0, 2000000.0, 65800000.0, 326000000.0, 60300000.0, 208000000.0, 1420000000.0, 25400000.0, 'Damages not recorded', 1540000000.0, 1240000000.0, 7100000000.0, 10000000000.0, 26500000000.0, 6200000000.0, 5370000000.0, 23300000000.0, 1010000000.0, 125000000000.0, 12000000000.0, 29400000000.0, 1760000000.0, 720000000.0, 15100000000.0, 64800000000.0, 91600000000.0, 25100000000.0]

I’m assuming this is where the issue is coming in, but not sure how everyone else was able to complete the project with the same data set. I know there is probably something small I missed.

hurricanes[i] is a mix of all the lists into one dictionary so maybe I’m trying to reference the wrong thing?

Current Project - not sure if this will help. This is my first time posting on the forums, I apologize if this is too much.

I used your code (but changed the print at the end) and it ran just fine.

your code (only the relevant parts)
# 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]

#convert damages to float
conversion = {"M": 1000000, "B": 1000000000}
def damages_conversion(damages): 
    updated_damages = []
    for i in damages: 
        if i[-1] == "M": 
        elif i[-1] == "B":
    return updated_damages

updated_damages = damages_conversion(damages)

#create hurricane dictionary 
def hurricane_dict(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths):
    hurricanes = dict()
    for i in range(len(names)): 
        hurricanes[names[i]] = {'Name': names[i], 'Month': months[i], 'Year': years[i], 'Max Sustained Winds': max_sustained_winds[i], 
                               'Areas Affected': areas_affected[i], 'Damage': updated_damages[i], 'Deaths': deaths[i]}
    return hurricanes

hurricanes = hurricane_dict(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths)

def most_expensive_hurricane(hurricanes):
    most_expensive_hurricane = ''
    damage_cost = 0
    for i in hurricanes:
        if hurricanes[i]['Damage'] == 'Damages not recorded':
        elif hurricanes[i]['Damage'] > damage_cost:
            most_expensive_hurricane = i
            damage_cost = hurricanes[i]['Damage']
    return most_expensive_hurricane, damage_cost

max_dam = most_expensive_hurricane(hurricanes)
print("most expensive hurricane was", max_dam[0], "at", max_dam[1])

Although I wouldn’t recommend a function named most_expensive_hurricane also having a variable with that same name inside of it.

1 Like

So was my issue that I wasn’t referencing the correct thing when I was trying to print the function?

Ok, I will work on my naming! Still trying to get the hang of best practices. Thank you for the feedback :slight_smile:

1 Like

For reassurance, can we add a print statement in here to see what value it is hanging on?

elif print (hurricanes[i]['Damage']) # > damage_cost:

Wrangle with the syntax however you can manage it.

No, I don’t think that was the issue.

Also, note that the print function does not return anything.
so doing

deadlist_hurricane = print(most_deadly_hurricane(hurricanes))

does not do what you want it to do.

I was able to get things working. I was getting Name Errors all the sudden and I went back and it looked like there was an issue (that I probably created somehow) with my initial data and it was throwing off everything! Everything is printing as it should now. Thank you both for walking me through this.

1 Like