5/16 is_prime


#1

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

Can someone explain why the else: triggers a “is_prime(2) fails” error, but when i delete it and fix indentation it comes back correct?


#3

Add print statements in your code to follow what it ends up doing. Compare to what it should be doing.

You should also know what difference that makes to your code, or if not, then you’ve got something that you urgently need to figure out before moving ahead. (Can’t really write anything by guessing what does what, and you especially can’t fix something that you don’t understand the individual components of)

Very few things snap into place by simply having a small amount of possible configurations that can be shuffled around. One needs to put things where they belong.

You’re right to stop and think about this, but there’s one more step to take before asking, which is considering why you can’t answer that yourself, and you’ll find that the answer to that is that you haven’t fully figured out what the things you were changing, do. (if-statements, for-loops, return statements), and that tells you that there is something you can look at in order to answer your own question


#4

thanks for the life lesson


#5

try <= 2 instead of x < 2. There’s nothing telling the code how to treat x = 2


#6

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