For loop not operating how I expect (hurricane project)... help!

Hurricanes, also known as cyclones or typhoons, are one of the most powerful forces of nature on Earth. Due to climate change caused by human activity, the number and intensity of hurricanes has risen, calling for better preparation by the many communities that are devastated by them. As a concerned environmentalist, you want to look at data about the most powerful hurricanes that have occured. Begin by looking at the damages list. The list contains strings representing the total cost in USD($) caused by 34 category 5 hurricanes (wind speeds ≥≥ 157 mph (252 km/h)) in the Atlantic region. For some of the hurricanes, damage data was not recorded ("Damages not recorded"), while the rest are written in the format "Prefix-B/M", where B stands for billions (1000000000) and M stands for millions (1000000).Write a function that returns a new list of updated damages where the recorded data is converted to float values and the missing data is retained as "Damages not recorded".Test your function with the data stored in damages.

import re

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

# 1
def numberize(string_list):
    float_list = []
    for item in string_list:
        if "M" in item:
            item_split = re.findall('(\d+|[A-Za-z]+)', item)
            if len(item_split) == 2:
                float_value = float(item_split[0]) * 1000000
            elif len(item_split) == 3:
                float_value1 = (float(item_split[0]) + (float('0.' + item_split[1]))) * 1000000
        elif "B" in item:
            item_split1 = re.findall('(\d+|[A-Za-z]+)', item)
            if len(item_split1) == 2:
                float_value2 = float(item_split1[0]) * 1000000000
            elif len(item_split1) == 3:
                float_value3 = (float(item_split1[0]) + (float('0.' + item_split[1]))) * 1000000000
# Update Recorded Damages
conversion = {"M": 1000000,
             "B": 1000000000}

# test function by updating damages


ValueError Traceback (most recent call last)
/var/folders/zn/3p1r8mqj4xs4_kgjm6k49vlw0000gn/T/ipykernel_31529/ in
40 # test function by updating damages
—> 41 numberize(damages)

/var/folders/zn/3p1r8mqj4xs4_kgjm6k49vlw0000gn/T/ipykernel_31529/ in numberize(string_list)
28 float_list.append(float_value2)
29 elif len(item_split1) == 3:
—> 30 float_value3 = (float(item_split1[0]) + (float(‘0.’ + item_split[1]))) * 1000000000
31 float_list.append(float_value3)
32 else:

ValueError: could not convert string to float: ‘0.M’

I’ve followed my code so many times ahhhh!

Why is an M item from the list going through the B if statement?
Why is there an error on me calling the numberize function on damages?


# You wrote:
float_value3 = (float(item_split1[0]) + (float('0.' + item_split[1]))) * 1000000000

# It should be:
float_value3 = (float(item_split1[0]) + (float('0.' + item_split1[1]))) * 1000000000
1 Like

Amazing, thanks so much!!