mardi 26 novembre 2019

Changing Elif Statement to "Python-Like" Code

I have the understanding that Python is all about efficiency. How would I write the code below to be more like Python?

I thought of using a dictionary, but since the conditions for the elif statement are ranges, I don't think that's possible.

threshold_scale

Output: [1960, 648065, 1294170, 1940275, 2586380, 3232486]

if pop in range(threshold_scale[0], threshold_scale[1]):
    color = '#ff0000'
elif pop in range(threshold_scale[1], threshold_scale[2]):
    color = '#f6ff00'
elif pop in range(threshold_scale[2], threshold_scale[3]):
    color = '#00ff08'
elif pop in range(threshold_scale[3], threshold_scale[4]):
    color = '#0d00ff'
elif pop in range(threshold_scale[4], threshold_scale[5]+1):
    color = '#ff00f7'

Aucun commentaire:

Enregistrer un commentaire