6. is_prime - function fails on prime(0)


#1

Every time, I get the message "function fails at is_prime(0). It returns none when it should return False.
Why do I have such trouble solving these lessons? Is it just me or everyone because I keep having to come to the Q&A forum to solve them.

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

#2

How many iterations will that loop do when x is 0? And what do you therefore return?

What do you imagine it should be doing for x=0? Find out if it's doing those things by having it print out what it's doing as it goes, as if it were explaining to you each step it carries out

What does a return statement do? What can a function do after having returned?
If a loop always returns during an iteration, then how many iterations can it possibly be doing?
If a loop only ever iterates once, is it a loop at all?


#3

And, when you ask for help, make it about how you can solve the problem, not about solving the problem, because there's a vicious circle there to avoid (no clue if that's what's going on)


#4

I never truly understood what it meant to "iterate" through something. For x = 0, I would like it to return false because 0 is not a prime number. Why will my current code not do that when I specifically started the iterations after 2?


#5

Your code makes no mention of 2

iterate is similar to the word repeat, but repeat means to do the same thing, iterate means to do the next thing, like in a loop

Start by finding out how many times your loop iterates for x=0
If you execute it manually, in your head, how many times would you run the code in the loop?
What controls how many times it runs? And what does that end up as for x=0?

You need to understand in full what you've written, and this is where you'll want to start digging because this is where it falls apart.


#6

So I created a list in order to find how many times it iterates for x = 0 but only 'None" is getting printed on the console.
Here is the code:
prime = [0, 1, 2, 3, 4]
def is_prime(x):
for n in range(0, x - 1):
if x % n:
return False
else:
return True
is_prime(prime)
print prime


#7

Instead of asking "why doesn't it" - ask yourself why it would

Because whatever would cause it to do that something, that isn't happening, and that's the answer to "why doesn't it" - in other words, you already have the answer to that question.

If you don't know why it would behave you want, then that's saying that you have written something but you don't know what. That's.. a problem, so you would then have to start by deciding what it is you are writing, because otherwise you've got nothing to type

If you print something each iteration, and nothing is printed, then how many times did it iterate? However, the code you posted (isn't intact, please make sure it's intact or I'm not looking at what you ran, that defeats the purpose) doesn't have a print statement in a loop so it wouldn't print anything regardless of how many iterations were executed

You don't need to test anything though. Ask yourself what controls how many iterations there are. You need to fully understand that anyway


#8

I assume it iterated 0 times since nothing is printed.


#9

Except you aren't printing anything each iteration, so your test only has one single outcome, so it can't tell you anything


#10

Sorry this all is going over my head right now. I'm getting more and more confused xP


#11

Let's just exaggerate the problem then.

How many iterations will it do for the number -1000?
How many should it do?
How should -1000 be handled?


#12

Oh I just got it! I noticed what you meant about 2 not being mentioned and also realized that since return is in the loop, the iterations are not happening. I edited the code:

def is_prime(x):
   for x in range(2, x - 1):
      if x % n:
         print False
      else:
         print True

#13

So for x < 2, I would like it to return False since these numbers are not prime, as the hint states. I think I would need to add this to the if statement?


#14

I'm not making any conclusions for you!
Refer to how you would determine whether a number is prime

But now you're arguing for why things would or wouldn't be correct, which is exactly what you need to do about every single thing you write


#15

I changed the code to this:

def is_prime(x):
   for n in range(2, x - 1):
      if x % n:
         print False
      else:
         print True

Since it needs to return False if x is evenly divisible by n, I thought of adding the return statements at the end like so:

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

Maybe the return statements needed to be included inside the loops without breaking it somehow? Am I even on the right track :confused:


#16

Q What defines a non-Prime?
A Divisibility


#17

if x % n == 0
This would take care of that, I assume?


#18

After much changes, I was finally able to solve my code with little help! I'm really proud of myself.


#19