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

I'm trying to write a program that states the primes range(1,x) can't find the bug help? http://dpaste.com/607619/

OpenStudy (anonymous):

I was following your logic until this: in line 6 you say "divisor != 1" Then in line 7 you said that "divisor =1" Seems to me with that wording, nothing will ever pass as true, therefore that loop will never yield a result. I'm also having trouble understanding why you're resetting the divisor to 1 after "print (prime,'is a prime number before', pick)" Isn't that your final result? What would you be calculating after your final result?

OpenStudy (anonymous):

I've been learning about debugging, and a good method is to insert a bunch of print statements into your code (print prime, "prime", etc). You can find what is yielding results, what isn't, and where it's miscalculating stuff. And I've also figured out through trial & error that comments are a must in scripting. You write down what you intend a line to do, then when we (or you in a year) review it, everyone can understand more clearly why you chose what you did.

OpenStudy (anonymous):

mmm.... The for loop pick numbers between 2 and 6, For 2 line 6 is false, that's bad because 2 is a prime. Then divisor is 1 so line 10 is true your code makes divisor = 2 line 15 does nothing in theory.... so the for loop stars again wit prime = 3, and divisor = 2 line 6 is false again, line 10 makes divisor = 3 and the for loop starts again whit prime = 4 and divisor = 3... Sadly your code doesn't work probably you should try again

OpenStudy (anonymous):

looks as though the issue was the for loop. the way of finding primes was correct, but it's an exhaustive search, so the for control structure was running through each number once, when I needed it to run multiple times. this was my solution http://dpaste.com/607707/ it creates duplicates, so I'm still looking into that. thinking of coming back when I know more about lists. then I can put all the numbers in a list and delete any duplicates

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!