6/15, Please, explain


#1

Please just explain why won't it work???
def is_prime(x):
for i in range(2, x - 1):
if x % i == 0:
return False
break
elif x < 2:
return False
break
else:
return True
break


#2

Please ask a specific question.

You can for example explain why you think it works, line by line, and ask us where your code is different. Right now you're asking us to do a bunch of extra work so that you can do less.

You might also want to post the intact code.


#3

When I run it, it tells me that my code fails when it's passed zero(0). What's wrong with my code? I'm not asking you to give me an answer, I've just asked why it doesn't work.


#4

Try doing that and find out what happens.


#6

I won't even say why it doesn't work. But I'll help you find out, because then you'll be able to figure out other bugs as well, much more useful than just knowing what went wrong here.

Call your function with the argument the error mentions. Observe how it acts differently from what you think is correct.

If there's an error message in the console, then investigate that.

If the result is incorrect, consider what you think should happen step by step to get the right result, and then look at your code and see if that seems to match.

If you think it matches, then you've misunderstood your code, or your steps don't add up to the correct result.

If you've misunderstood what your code does, then you can add prints in your function, make the function print out what it is doing step by step, as if the function is explaining to you what it is doing. It should be fairly easy to then read the output and see where it goes wrong.


#7

Well, the thing is, I can't even tell how my code works, because the console window gives out nothing more than "None". Even if I wanted to print something out to the console window, it won't show. So, I understand that my question does sound idiotic, but the real problem is that there is no real way for me to tell what's wrong with my code because I can't see how it works. And the error message isn't enough for someone with little coding experience, like me.


#8

My only issue is in how you choose to formulate it, I much rather see "How do I approach this" along with code that isn't mangled and what you're seeing and trying to make happen, than.. That.

I think it's perfectly reasonable not to know how to deal with your error message, or even what information is useful to put in a question. Then start there. Asking questions is a lot like starting to solve it yourself. Get good at one of them and you'll be much better at the other as a result.

What you've been saying since is much much better.

If your code isn't printing anything then the absolutely most likely reason is that the control flow of your program simply doesn't come across any print statements. Perhaps your prints are in your function and you aren't calling it.


#9

So like:

Here's my code
<code>

And I have been trying to print out what it's doing but for some reason nothing is printed and I don't know why, what am I missing?

^ great


#10

print "And I have been trying to print out what it's doing but for some reason nothing is printed and I don't know why, what am I missing?"

Haha, very funny.


#11

Also, can I delete this topic if it's created by me?