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

I haven't got the faintest idea why my 1A code isn't working. When I run it, it doesn't print out anything at all. http://pastebin.com/GsVZ5uNF

OpenStudy (anonymous):

I'm kinda new at this (have been struggling with the same problem), but I think b/c you included "1" in your "allprimes", it will always break.

OpenStudy (anonymous):

thearchie has got it, every number is divisible by one (including prime numbers :-P) Besides, 1 isn't really defined as a prime number.

OpenStudy (anonymous):

oh dear god, that's so simple. but! i fixed it and it still isn't working. anything else up with it?

OpenStudy (anonymous):

(ps. thanks!)

OpenStudy (anonymous):

Something that helps me is to insert all kinds of print statements into the code. When you run it, you may find the error a little easier because something won't print correctly or at all.

OpenStudy (anonymous):

I inserted some print statements after the "for" and you have an infinite loop going on (or at least a loop that runs for a lot longer than I was willing to let it run). Actually, I'm in the process of figuring out how loops really work, so I'm afraid any help I could give stops here.

OpenStudy (anonymous):

http://codepad.org/eWUHFmBl The [1] in your allprimes wasn't the only problem with your code; it doesn't properly test every member of allprimes against the candidate. Also, it does not properly change the candidate if it fails the test. To fix those, I moved 'candidates = candidates + 2' (candidates += 2 does the same thing) one indent block back so it's lined with the while loop, meaning it will always change candidates once a number has been tested for primality whether it passes or not. Another problem was that there were duplicate prime numbers in the allprimes list showing up due to candidates passing the test multiple times against all candidates of allprimes. I decided to use a boolean that would set to False if a candidate wasn't prime and then moving the allprimes.append(candidate) instruction after the for loop, and added an if statement so it only adds numbers that are prime to the list. I thought 'if len(allprimes) == 1000' was unnecessary so I took it out :-P It works perfectly now. Try 'print allprimes' and compare the list to the list of primes included with the problem set.

OpenStudy (tonybaldwin):

Darn...you did it in 12 ilnes, and it took me 15. I didn't even use this boolean method, and I actually had to put q = q + 2 in there twice. Having the same line twice is redundant, but it was the only way I could make it work. I'm not impressed with myself... Here's my lame code: http://codepad.org/JE9bHUfJ

OpenStudy (tonybaldwin):

Oh, worse, I even had to put q = q - 2 to print my final result correctly, since I'd added 2 to it already...weird....

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!