mardi 6 février 2018

Else outside indentation Not Giving Error [duplicate]

This question already has an answer here:

I'm using the following code to find the prime number from a list, like so:

list_int = [2434,54,2,34,5,76,87,9,23,54]

#OUTSIDE BLOCK
for num in list_int:

#FIRST IF STATEMENT
if num > 1:

    #MIDDLE BLOCK
    for i in range(2, num):

        #SECOND IF STATEMENT
        if (num % i) == 0:
            print(num, "is not a prime number")
            print(i, "times", num // i, "is", num)
            #BREAK
            break

    #ELSE OF THE SECOND IF IS OUTSIDE IF INDENTATION BUT STILL WORKING FINE???
    else:
        print(num, "is a prime number")
# ELSE OF THE FIRST IF 
else:
    print(num, "is not a prime number")

How can an else statement be outside of its indentation, as I was sure that If, ELIF, ELSE needs all to be in the same block they belong to or am I missing something here?

Aucun commentaire:

Enregistrer un commentaire