samedi 22 avril 2017

if/else statement syntax error?

I try to print prime number that's inside given interval from users. As of logic case, the 'else' statement should also be inside for loop. But if I put like that, it doesn't print desired output. Can you explain how the code work? Please give some explanation. Thank beforehand :D

inp1 = int(input("Enter first number: "))
inp2 = int(input("Enter second number: "))
print("Prime between {} and {} = " .format(inp1, inp2))

if inp1 > inp2:
    temp = inp2
    inp2 = inp1
    inp1 = temp

for num in range(inp1, inp2 + 1):
    if num >1:
        for i in range(2, num):
            if(num % i) == 0:
                break
        else:
            print(num)

Aucun commentaire:

Enregistrer un commentaire