-3

I cannot understand why Python is not doing what I am asking it to.

The method:

def IdentifyUVIndex(self, UVType):

        if (UVType >= '1' and UVType <= '2'):

            return "Low Exposure."

        elif (UVType >= '3' and UVType <= '5'):

            return "Moderate Exposure."

        elif (UVType >= '6' and UVType <= '7'):

            return "High Exposure."

        elif (UVType >= '8' and UVType <= '10'):

            return "Very High Exposure."

        elif (UVType >= '11'):

            return "Extreme Exposure."

        else:
            return "Unknown."

So if I type:

print main().IdentifyUVIndex('1')

it returns Low Exposure. Great! But as soon as I pass anything greater than 7 it immediately returns "Extreme Exposure".

Have I done something wrong? It should return Very High Exposure.

And if I pass '11' it should return extreme exposure but returns low exposure? This is so confusing!!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

0

You are comparing strings, not integers:

>>> '1' <= '11' <= '2'
True
>>> 1 <= 11 <= 2
False

Convert your input number to an integer and fix code accordingly.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251