Prime numbers


#1



. Loops: Practise Makes Perfect: 6. is_prime


All answers seem to return 'None'


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

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


#2

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


#3

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'


#4

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


#5

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