mercredi 4 avril 2018

Prime Number Generator - Different Indentation for the else block [duplicate]

This question already has an answer here:

for the solution below, why does the "else" block have a different indentation from the if block? Thanks for the all the help!

prime_numbers = []

def prime_gen(upper_limit):
    for i in range(2, upper_limit):
        for j in range(2, i):             # <== only look for divisors less than i
            if i % j == 0:                # <== STOP if you found a divisor
                break
        else:                             # <== Add only if no divisor was found
            prime_numbers.append(i)
prime_gen(20)
print(prime_numbers)

Aucun commentaire:

Enregistrer un commentaire