-2

I am currently using python3.9. I have coded a python block of code in which I have put an if else Condition but when I enter my input such as 15 then both conditions became true like the following. I want to know how an if-else condition can be true in both case. You can see it in following screenshot so that you can understand well and help me in this.:

Here is the Image I am giving you for inspecting.

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)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 3
    Perhaps you should add: `print(i)` before each `print()` statement to see what is really going on. – quamrana May 10 '21 at 14:15
  • 3
    Check the for loop. Print the number just after the for loop and things might become clear. – Pam May 10 '21 at 14:16
  • Have you done any debugging? I would recommend reading https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – AMC May 10 '21 at 14:52

4 Answers4

1

you are checking multiple times, on one iteration condition may return one aswer, on next - other, in your case firstly it is divisble by 1, prints "It is consecutive", then not divisble by 2 and prints "It is prime", then meets break statement, make an individual check for <2 and then iterate over numbers, and then if it does not return anything print "It is prime", like this

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)
Allure
  • 513
  • 2
  • 12
0

The result you are getting is because of the for loop . For the first iteration the if condition evaluates to true (15 % 1 == 0). So "It is consecutive" is printed , the remaining elif and else conditions are not checked for For the second iteration, if condition is false (15 % 2 == 0 is false) .It goes to elif condition (y < 2) which is false too , since 15 < 2 is false . The flow goes into the else block and does whatever is specified in it (in this case prints "it is prime" and breaks from the loop ) Hence you get both the statements. Conclusion - The if-else block isn't executing simultaneously . It is due to the for loop the condition changes and hence the result

Astro
  • 11
  • 5
0

So your for-loop is doing the following:

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")]

I suspect you want something more like the following:

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)
0

It isn't True in multiple if branches. but for different values of the for loop. If that's code is a primality test, it should be more like that

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
azro
  • 53,056
  • 7
  • 34
  • 70