Prime numbers


. Loops: Practise Makes Perfect: 6. is_prime

All answers seem to return 'None'

def is_prime(x):
    if x < 2:
        return False
        for n in range(2, x):
            if x % n == 0:
                return False
                return True

Is putting a function parameter as an argument of range (I hope I got that right...:confounded:) not allowed?


A function ends the moment a return keyword is reached

If the return keyword is reached in a loop, the loop will break

so you should return True after the loop


Thanks! So 'else' statements are essentially pointless in these situations? As you can just put the else statement contents outside of the loop without declaring 'else'


Yea, the are a bit pointless indeed in this specific case


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.