2 questions about Python Hurricane Project, Step 8

Hi! I’m working on the Python Hurricane Project. I’ve finished step 8 and I have two questions.

  1. Since the morality scale is in a dictionary already is there a way to place the correct hurricane in the correct position in the scale using the mortality_scale dictionary without using all the if/elif/else statements?

  2. I understand why the incorrect function version is incorrect but why does it output each letter of each hurricane as its own list item?

# 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'] # 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] hurricane_deaths = list(zip(names, deaths)) mortality_scale = {0: 0, 1: 100, 2: 500, 3: 1000, 4: 10000} # This is the correct version def hurricane_mortality_scale(list): hurricanes_by_mortality = {0:[],1:[],2:[],3:[],4:[],5:[]} for entry in list: if entry[1] >= 10000: hurricanes_by_mortality[4].append(entry[0]) elif 1000 <= entry[1] < 10000: hurricanes_by_mortality[3].append(entry[0]) elif 500 <= entry[1] < 1000: hurricanes_by_mortality[2].append(entry[0]) elif 100 <= entry[1] < 500: hurricanes_by_mortality[1].append(entry[0]) else: hurricanes_by_mortality[0].append(entry[0]) return hurricanes_by_mortality hurricane_mortality_scale_dictionary = hurricane_mortality_scale(hurricane_deaths) print(f"Correct dictionary is: {hurricane_mortality_scale_dictionary}") # This is the incorrect version def hurricane_mortality_scale_incorrect(list): hurricanes_by_mortality = {0:[],1:[],2:[],3:[],4:[],5:[]} for entry in list: if entry[1] >= 10000: hurricanes_by_mortality[4] += entry[0] elif 1000 <= entry[1] < 10000: hurricanes_by_mortality[3] += entry[0] elif 500 <= entry[1] < 1000: hurricanes_by_mortality[2] += entry[0] elif 100 <= entry[1] < 500: hurricanes_by_mortality[1] += entry[0] else: hurricanes_by_mortality[0] += entry[0] return hurricanes_by_mortality hurricane_mortality_scale_dictionary_incorrect = hurricane_mortality_scale_incorrect(hurricane_deaths) print(f"incorrect dictionary is: {hurricane_mortality_scale_dictionary_incorrect}")

In hurricanes_by_mortality[4] += entry[0]
entry[0] is a string, not a list
and only lists can be added to lists using the + or +=
so that string is made into a list
'Cuba I' becomes ['C', 'u', 'b', 'a', ' ', 'I']

You can create a list containing only one item if you just want to append that one item to the list.
hurricanes_by_mortality[4] += [entry[0]]

I guess you could use an inner loop to iterate through mortality_scale if you want to have fewer if and elif statements.

possible code
def hurricane_mortality_scale(list):
    maximum = max(mortality_scale.keys())
    hurricanes_by_mortality = {cat:[] for cat in range(0, maximum + 1, 1)}
    for entry in list:
        for cat in range(maximum, -1, -1):
            if entry[1] >= mortality_scale[cat]:
                hurricanes_by_mortality[cat].append(entry[0])
                break  # needed so that is not appended for multiple categories
        # after break, does next entry in list
    return hurricanes_by_mortality
1 Like