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

I am currently using python version 2.5.4 and every time I try to use the "else" function it I get a syntax error. Why would I get this error? "SyntaxError: invalid syntax" Thanks in advance

OpenStudy (anonymous):

Can you write out an example?

OpenStudy (anonymous):

ok here is an example: I did create the problem myself but in every problem I write else: I always get this same response: >>> limit = 1000 >>> counter = 1 >>> current_number = 1 >>> while counter < limit: is_prime = 1 current_number = current_number + 2 for i in range (2, current_number%i) == 0: is_prime = 0 break if is_prime != 0: counter = counter + 1 if counter == limit: print current_number, "is the 1000th prime number" else: SyntaxError: invalid syntax

OpenStudy (anonymous):

The else function is colored in red to show that there is a syntax error there.

OpenStudy (anonymous):

Well, here the else: is tabbed incorrectly. It needs to be lined up with the if: if is_prime != 0: counter = counter + 1 if counter == limit: print current_number, "is the 1000th prime number" else: Let me know if that solved it.

OpenStudy (anonymous):

yes, that was the problem thanks for the help.

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!