Home > database >  How do I make my function to process the str first?
How do I make my function to process the str first?

Time:02-04

I have this function:

def damage_rate(hurricanes):
    damage_scale = {0: 0,
                    1: 100000000,
                    2: 1000000000,
                    3: 10000000000,
                    4: 50000000000}
    hurricanes_by_damage = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[]}
    for cane in hurricanes:
        total_damage = hurricanes[cane]['Damage']
        if total_damage == "Damages not recorded":
            hurricanes_by_damage[0].append(hurricanes[cane])
        elif total_damage == damage_scale[0]:
            hurricanes_by_damage[0].append(hurricanes[cane])
        elif total_damage > damage_scale[0] and total_damage <= damage_scale[1]:
            hurricanes_by_damage[1].append(hurricanes[cane])
        elif total_damage > damage_scale[1] and total_damage <= damage_scale[2]:
            hurricanes_by_damage[2].append(hurricanes[cane])
        elif total_damage > damage_scale[2] and total_damage <= damage_scale[3]:
            hurricanes_by_damage[3].append(hurricanes[cane])
        elif total_damage > damage_scale[3] and total_damage <= damage_scale[4]:
            hurricanes_by_damage[4].append(hurricanes[cane])
        elif total_damage > damage_scale[4]:
            hurricanes_by_damage[5].append(hurricanes[cane])
    return hurricanes_by_damage

But when I try to print it in Jupyter, it keeps telling me that '>' is not supported between str and int.

I already tried a different method of ignoring the str in the library with pass but it didn't work either.

CodePudding user response:

Convert the value to int before comparing and it should resolve the issue. Keep in mind the edge cases where the value is not a numeric value other than "Damaged not recorded" would throw another error that needs to be handled.

In your case, the simple fix would be:

def damage_rate(hurricanes):
damage_scale = {0: 0,
                1: 100000000,
                2: 1000000000,
                3: 10000000000,
                4: 50000000000}
hurricanes_by_damage = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[]}
for cane in hurricanes:
    total_damage = hurricanes[cane]['Damage']
    if total_damage == "Damages not recorded":
        hurricanes_by_damage[0].append(hurricanes[cane])
    
    else:
        try:
            total_damage = int(total_damage) #converting to integer to compare
        except Exception as E:
            print(E)
            print(f"Cant convert '{total_damage}' to int") #add code to handle error here

        if total_damage == damage_scale[0]:
            hurricanes_by_damage[0].append(hurricanes[cane])
        elif total_damage > damage_scale[0] and total_damage <= damage_scale[1]:
            hurricanes_by_damage[1].append(hurricanes[cane])
        elif total_damage > damage_scale[1] and total_damage <= damage_scale[2]:
            hurricanes_by_damage[2].append(hurricanes[cane])
        elif total_damage > damage_scale[2] and total_damage <= damage_scale[3]:
            hurricanes_by_damage[3].append(hurricanes[cane])
        elif total_damage > damage_scale[3] and total_damage <= damage_scale[4]:
            hurricanes_by_damage[4].append(hurricanes[cane])
        elif total_damage > damage_scale[4]:
            hurricanes_by_damage[5].append(hurricanes[cane])
return hurricanes_by_damage

CodePudding user response:

The issue is that the values in the damage_scale dictionary and the total_damage variable are of different data types. damage_scale has integer values, while total_damage is a string. This causes the comparison operations (>, <=, etc.) to fail with a "not supported between instances of 'str' and 'int'" error.

You need to make sure that total_damage is of the same data type as the values in the damage_scale dictionary. You can do this by converting total_damage to an integer before doing the comparison operations. For example:


def damage_rate(hurricanes):
    damage_scale = {0: 0,
                    1: 100000000,
                    2: 1000000000,
                    3: 10000000000,
                    4: 50000000000}
    hurricanes_by_damage = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[]}
    for cane in hurricanes:
        total_damage = hurricanes[cane]['Damage']
        if total_damage == "Damages not recorded":
            hurricanes_by_damage[0].append(hurricanes[cane])
        else:
            total_damage = int(total_damage)
            if total_damage <= damage_scale[0]:
                hurricanes_by_damage[0].append(hurricanes[cane])
            elif total_damage <= damage_scale[1]:
                hurricanes_by_damage[1].append(hurricanes[cane])
            elif total_damage <= damage_scale[2]:
                hurricanes_by_damage[2].append(hurricanes[cane])
            elif total_damage <= damage_scale[3]:
                hurricanes_by_damage[3].append(hurricanes[cane])
            elif total_damage <= damage_scale[4]:
                hurricanes_by_damage[4].append(hurricanes[cane])
            else:
                hurricanes_by_damage[5].append(hurricanes[cane])
    return hurricanes_by_damage

This code first checks if total_damage is "Damages not recorded". If it is, it appends the hurricane to the hurricanes_by_damage[0] list. If it's not, it converts total_damage to an integer and does the comparison operations using integer values.

  • Related