【问题标题】:Python If elif elsePython If elif else
【发布时间】:2018-02-14 03:41:43
【问题描述】:

`

is_summer = True
 print("Values 1-12 correlate to the months of the year")
 while is_summer == True:
     _month = int(input("Enter the month of the year: "))
     if _month == 4 or 5 or 6 or 7 or 8:                 
         is_summer = int(input("It is summer, what is the temperature?: "))
         if is_summer in range(60,101):                  
             print("The squirrels are playing")
         else:
             print("The squirells are not playing")      
     elif _month == 1 or 2 or 3 or 9 or 10 or 11 or 12:   
         is_summer = int(input("It is winter, what is the temperature?: "))
         if is_summer in range(60,91):
             print("The squirrels are playing")           
         else:
             print("The squirrels are not playing")
    `

如果我输入 1、2、3、9、10、11 或 12,我的代码将不会进入 elif 语句。是我的嵌套 if 语句执行不正确,还是其他原因?

【问题讨论】:

  • 更优雅的解决方案是使用in 运算符。

标签: python loops nested


【解决方案1】:

您的代码未按预期执行的原因是您实际上并未检查_month 是否等于每个数字。

if _month == 1 or 2 or 3if _month == 1 or _month == 2 or _month == 3 不同。

将第一个视为if (_month == 1) or (2) or (3)_month == 1 的计算结果为 False,但 23 是非零值,计算结果为 True,因此始终采用第一个 if

【讨论】:

    【解决方案2】:

    您的条件语句如下工作,这就是您提供的输入条件并不总是正确的原因。

    >>> month = 4
    >>> month == 4 or 5 or 6
    True
    >>> month = 5
    >>> month == 4 or 5 or 6
    5
    

    您可以使用in 运算符来检查_month 是否存在于您使用or 检查的值列表中。 in 运算符的工作原理如下

    month = 1
    month in [1,2,3,4,5] # True
    month in [2,3,4,5,6] # False
    

    因此您可以将程序更改为

    is_summer = True
    print("Values 1-12 correlate to the months of the year")
    while is_summer == True:
        _month = int(input("Enter the month of the year: "))
        if _month in [4,5,6,7,8]:                 
            is_summer = int(input("It is summer, what is the temperature?: "))
            if is_summer in range(60,101):                  
                print("The squirrels are playing")
            else:
                print("The squirells are not playing")      
        elif _month in [1,2,3,9,10,11,12]:   
            is_summer = int(input("It is winter, what is the temperature?: "))
            if is_summer in range(60,91):
                print("The squirrels are playing")           
            else:
                print("The squirrels are not playing")
    

    【讨论】:

      【解决方案3】:

      你的问题在于你的 if 语句。

      if _month == 4 or 5 or 6 or 7 or 8: 
      

      这会检查 _month == 4,或者 5 是否为真,或者 6 是否为真,等等。

      你想做的事:

      if _month == 4 or _month ==5 or _month == 6 or _month == 7 or _month == 8:
      

      或者更简洁

      if 4 <= _month <= 8:
      

      对你的 elif 做同样的事情。虽然如果你知道 _month 将是从 1 到 12,那么实际上它可能只是一个 else,而不是一个 elif

      【讨论】:

        【解决方案4】:

        您的第一个if 声明基本上是在说明_month 是否为4——很好——或者如果5、6、7 或8 将评估为True。在 Python 的 if 语句中,正整数的计算结果总是True

        if 1:
          print("hello!")
        

        将始终打印hello!。您的 if 语句需要如下所示:

        if _month == 4 or _month == 5 or _month == 6 or _month == 7 or _month == 8:
          print("hurrah!")
        

        然而,这变得不必要地冗长了——我们可以使用比较运算符来简化它,例如小于 (&gt;) 和大于 (&lt;),如下所示:

        if _month < 3 and _month > 9:
          print("hurrah!")
        

        【讨论】: