【问题标题】:What is happening to my python program, please tell me? How both if else statement can be true? [duplicate]我的python程序发生了什么,请告诉我? if else 语句如何都为真? [复制]
【发布时间】:2021-07-31 22:53:56
【问题描述】:

我目前正在使用python3.9。我编写了一个 python 代码块,其中我放置了一个if else 条件,但是当我输入我的输入(例如15)时,两个条件都变为真,如下所示。我想知道if-else 条件在这两种情况下如何为真。您可以在以下屏幕截图中看到它,以便您更好地理解并帮助我。:

x = input("Enter a number: ")
x = int(x)

def my_func(y):
    for i in range(1, y):
        if y % i == 0:
            print("It is consecutive")
        elif y < 2:
            print("It is smaller than 2")
        else:
            print("It is prime")
            break


my_func(x)

【问题讨论】:

标签: python if-statement pycharm


【解决方案1】:

您正在检查多次,在一个迭代条件下可能返回一个 aswer,在下一个 - 另一个上,在您的情况下,首先它可被 1 整除,打印“它是连续的”,然后不能被 2 整除并打印“它是prime”,然后遇到 break 语句,对

x = input("Enter a number: ")
x = int(x)


def my_func(y):
    if y<2:
        print("It is smaller than 2")
        return
    else:
        # A prime will not have any factors greater than 1
        for i in range(2, y):
            if y % i == 0:
                print("It is consecutive")
                return
        print("It is prime")
my_func(x)

【讨论】:

  • 请注意,i = 1 将始终将 y % i == 0 设为 True
【解决方案2】:

你得到的结果是因为 for 循环。 对于第一次迭代,if 条件的计算结果为真 (15 % 1 == 0)。所以“它是连续的”被打印出来,剩余的 elif 和 else 条件不被检查 对于第二次迭代,如果条件为假 (15 % 2 == 0 为假)。它进入 elif 条件 (y

【讨论】:

    【解决方案3】:

    所以你的 for 循环正在执行以下操作:

    for i in range(1, 15): # Iterate over 1,2,3,4,..
        # 1 - [(15 % 1) = 0, True, print("It is consecutive")
        # 2 - [(15 % 2) != 0, False...
        #      (y < 2), False...
        #      print("It is prime")]
    

    我怀疑你想要类似以下的东西:

    x = input("Enter a number: ")
    x = int(x)
    
    def my_func(y):
        # Any number less than 2 is not prime
        if y < 2:
            print("It is smaller than 2")
            # Exit the function early and don't continue onto the for loop
            return
        for i in range(2, y):
            # if this evaluates as True for any of i < y then y is not prime
            if y % i == 0:
                print("It is consecutive")
                # Exit the function and do not complete the for loop
                return
        # If we have reached here then the for loop has executed fully and no values of i have been found to be factors of y, therefore y is prime
        print("It is prime")
    
    my_func(x)
    

    【讨论】:

      【解决方案4】:

      它不是多个if 分支中的True。但对于 for 循环的不同值。如果那个代码是素数测试,应该更像这样

      def my_func(y):
          if y < 2:
              print("Lower than 2")
              return
          for i in range(2, y):
              if y % i == 0:
                  print("It is consecutive, because of", i)
                  break
          else:
              print("It is prime")
      
      # else is triggered if no `break` used
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多