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
Can you write out an example?
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
The else function is colored in red to show that there is a syntax error there.
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.
yes, that was the problem thanks for the help.
Join our real-time social learning platform and learn together with your friends!