Python ELIF Bug?

2024-06-26 00:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白为什么Python没有按照我的要求去做。你知道吗

方法:

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."

如果我输入:

print main().IdentifyUVIndex('1')

它的曝光率很低。太好了!但是当我通过任何大于7的值时,它立即返回“极端暴露”。你知道吗

我做错什么了吗?它应该返回非常高的曝光率。你知道吗

如果我通过了11,它应该会返回极端曝光,但返回低曝光?这太令人困惑了!!你知道吗


Tags: and方法selfreturnifdefverylow
1条回答
网友
1楼 · 发布于 2024-06-26 00:28:07

您正在比较字符串,而不是整数:

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

将输入的数字转换为整数并相应地修复代码。你知道吗

相关问题 更多 >