def is_prime(x): if x < 2: return False n = 2 while n < x: if x % n == 0: return False n += 1 return True
What’s wrong with this program? It isn’t giving me an error message about what I failed on, yet I can’t progress.
All numbers that are less than 2 are false, so it returns false and ends the function if x is less than 2. it defines n as 2, and runs a while loop until n is equal to x, in which case it returns True. The while loop checks all numbers between 2 (included) and x - 1 (included), and if x divided by that number doesn’t have a remainder, it returns False and ends the function. And if the number is 2, it just skips the while loop and returns True.
EDIT: I just ran it again and it’s working now. I swear I didn’t change anything and I’ve ran it again and again before, so I really don’t know how it’s working now. Not sure how to delete this post.