My while loop is being ignored

I am working on point 8 in the hurricane project on the Data Science pathway. I could probably find a better way but I now just want to know why this code doesn’t work!! Hence why I tagged this as general.

The objective is to check mortality figures for their storms against the mortaliy_scale and store the storm with other storms of the same mortality scale.

I added print statements to track what is actioned and what isn’t. The dictionary providing storm names with their mortality figure is being accepted and the code appending to the list mortality_dict has worked in other routes I have tried. It iterates through the list of storms but skips past the while loop.

Probably quite simple but I’m just stuck! Please can I have some ideas or insight into the problem?

# 7
# Rating Hurricanes by Mortality
print('\n\n Mortality Scale')
def by_mortality(storm_info):
  mortality_scale = {0: 0, 1: 100, 2: 500, 3: 1000, 4: 10000}
  mortality_dict = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[]}
  for storm in storm_info:
    print('for loop ok') # these prints are to see which sections are working and keep track. I'll remove them at the end.
    mort_key = 4
    while mort_key > 0 == True:
      print('While loop ok')
      if storm_info[storm]['Deaths'] > mortality_scale[mort_key]:
        scale_value = (mort_key + 1)
      elif storm_info[storm]['Deaths'] == 0:
        mort_key += -1
    print('??') # this prints, but the while loop doesn't

    # else:
      # mortality_dict[mort_key].append('No value')
  return mortality_dict
by_mortality = by_mortality(storm_dict)

Your loops not being ignored, its condition is just not being evaluated as True.

Currently you’re using the condition mort_key > 0 == True, and (to my knowledge here) this will always result to False due to precedence problems.

Typically if you want to check if one value is more than another you won’t use an equality operator, just a more than. Though if you ever end up needing to it is a good idea to include parenthesis:

a = 5
b = 4
if (a > b) == True:

I’m unaware of the exact science of how a conflict is handled when two operators with the same precedence are handled, so if someone with more knowledge than I have wants to step in, please feel free.


In python, comparisons can be chained arbitrarily (all comparison operations have the same priority). This means that mort_key > 0 == True is equivalent to mort_key > 0 and 0 == True. While the first statement would appear to evaluate to True when you first look at it, the second one evaluates to False. This is why your while loop isn’t executing.

If you remove the == True, the condition at the start of your loop will evaluate to True and the loop will execute.


@microrunner33748 covered this nicely but just in case you want the docs-


Thank you! This makes total sense and is reassuring, as I had tried that first and got no output.

Looking at it again it seems to be an infinite loop. I’ve loaded into VS code and that lets me see more of what is happening so I can try to pick it apart.