Again, I come looking for aid.
I’ve passed the exercise, but need some help better understanding why. Here is what I used to satisfy the prime number function.
def is_prime(x): if x < 2: return False for n in range(2, x): if x % n == 0: return False else: return True
I ran into great frustration trying to have the else as apart of the if statement within the for-loop. This is because the return False was breaking the loop and preventing the else from being executed. The solution was to attach the else statement to the first if statement.
The way I believe it to operate is to first check if x < 2, check the for-loop if x >= 2, then run the else portion of the if/else statement if previous conditions are not satisfied. I’m convinced the if/else doesn’t take precedent over the for-loop as the program works down through the block of the function. Why? I’m not entirely certain.
And is there anyway to prevent the return False from breaking the loop so it can be apart of the if/else within the for-loop?
Thank you very much,