A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Can you write out an example?

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes, that was the problem thanks for the help.

  6. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.