Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

Assignment 1 1000th prime code. I think i have some sort of semantic error going on,and i am completely stuck on this. http://codepad.org/bczvKExW

OpenStudy (anonymous):

The only time "isprime" is true is on the first prime number. That "for" part will keep looping because it will never become "true" again. I think that it might work if you put the statement "isprime = true" in the beginning of the "for" condition, that way, it won't be an infinite loop. I'm just a noob at programming though, so this might not work, lol.

OpenStudy (maitre_kaio):

Here are the values of your variables at the start: counter = 0, isprime = True, testnumber = 1 Since counter < 1000, the interpreter goes in the while loop. You say x in range(2, testnumber) which evaluates to range(2,1), which is an empty list. So it never enters the for loop and counter is never incremented. Hence the infinite loop.

OpenStudy (anonymous):

Such a simple mistake,and i thank you greatly maitre_kaio,so here is my updated version,its a basic generator of primes without input.Advice would be a pleasure. http://codepad.org/SQEvhoCw

OpenStudy (anonymous):

I thinks it's simple (which is good), my only comment: for readability you should indent your comments on the same level as your code.

OpenStudy (anonymous):

Thanks! I was trying to go for simple,ill be sure yo indent as well next time.

OpenStudy (anonymous):

If you want to know more about readability for python, these are some guidelines: http://www.python.org/dev/peps/pep-0008/

OpenStudy (maitre_kaio):

Now look at your results. Are all these results really primes ?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!