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

In PS1 please explain why this is not working: http://chopapp.com/#gzvnvu8s

OpenStudy (anonymous):

For your line 6 if block, you need to add a line that continues to add to x (x +=1). The way its set up now, all it does when it reaches that line (it should reach it when x reaches 4) is break, which means none of the variables change and x stays at 4 and counter stays at 2 forever, meaning your program never leaves the while loop.

OpenStudy (anonymous):

Still stuck in infinite loop: http://chopapp.com/#k3ftwm2y

OpenStudy (anonymous):

I'm going to give you an extremely valuable debugging tip. It helped me when I was doing this problem set, and it'l help you in every other problem set after it. It's way more valuable than me actually giving you the answer, trust me. Just add some print statements showing the values of the variables at each point in the loop. That's it! In this case, you can add print x, counter, m, x%m on each conditional block (be sure to place it before the break on your if block of course). What this will do will print out the current values of each variable as the loop runs. With this, even if the program loops forever, it'l show you every single step the program takes, so you can easily see where it goes, and whereever you see it doing something wrong, you'll know that's where it gets stuck! So all you have to do is check why those variables at that point are causing it to loop, and you can just run through your code in your head seeing where its ending up. Good luck!

OpenStudy (anonymous):

hello andreipop. the line of code range(2, x-1) shoots out a blank list [] since range function creates a list of numbers from the first input and the (last input - 1). So, just change your range to range(2, x), and you shouldn't be stuck in infinite loop

OpenStudy (anonymous):

And to add to kgiax's tips, debug your program while searching for only the first 5 primes. You'll have a lot less output to sift through when things aren't working. Once you get that working, try a slightly larger number, like 10 or 20. Once you know all that is working, crank it to 1000.

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!