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

I am having troubles with ps1a, listing the prime numbers. Here is my code which errors out - import math int = 1 while int <= 1000: x = math.sqrt(int): if x != 0 print (int, 'is a prime number') else int + 1 http://pastebin.com/YFveDGqC The error I am getting is a syntax error at the end of line 4. I am using python 3 if that makes a difference.

OpenStudy (anonymous):

Let me start with your last question. Yes, Python 3 does make a difference. I haven't used Python 3, only Python 2.6 so I can't tell you for sure that this shouldn't work on 3, but I know that they are completely different in code and syntax. In the video courses they use 2.6 so I'll recommend using that one. about the code itself, I copied it to my environment (2.6) and I get the same error. The reasons is, that "x = math.sqrt(int):" is not an 'if', 'while' or 'for' block. You define a variable. I'm not sure exactly what you were trying to do, but it seems that the best way to fix it is to remove the ':' and remove 1 tab from each of the lines 5 to 8. However, this will still just be an endless loop. I don't really understand why do you use the sqrt function, a prime number is a number that does not divide without a remainder. you should this in a direction of creating a sub loop that checks (int % i == 0) when i will be all the numbers from 2 to int (dividing by a larger number will not be without a remainder) and if the loops reaches the end without finding a true value for the above condition, it should say that int is a prime number. good luck!

OpenStudy (anonymous):

I think the problem might be the ':' at the end of line 4, it doesn't go there. Also I think there should be a colon at the end of line 5. Some of the commands' syntax is different in Python 3 so it's better to use Python 2.x. Also pylab and matplotlib (lecture 17) don't work with Python 3.x.

OpenStudy (anonymous):

Don't name your variable 'int' int is a builtin datatype, and calling your variable int will mask this data type. Also 'int' isn't very descriptive. It may explain what the variable is, but it doesn't say what it is for. Use descriptive names like 'prime_candidate' or 'possible_prime'

OpenStudy (anonymous):

Your main problem is that your test for primes isn't correct at all. Just because the square root of a number isn't 0, doesn't mean it's a prime number.

OpenStudy (anonymous):

The biggest difficulty I see in the first problem set is people getting their variables mixed up. Keep careful track of which number you are testing for primeness and which number you are using as a divisor.

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!