-1

I have recently started using Python and I was typing out a simple code when I got an Invalid syntax error does anyone understand where I went wrong?

Swansea= 2

Liverpool= 2

If Swansea < Liverpool:
    print("Swansea beat Liverpool")

If Swansea > Liverpool:
    print("Liverpool beat Swansea")

If Swansea = Liverpool:
    print("Swansea and Liverpool drew")

The word "Swansea" becomes highlighted in red

1
  • 4
    Another point that doesn't relate to your syntax error, is that when you are comparing with <,>,and ==, there is only one possible answer. A number can't be < and > and == another number. So it would make more sense to you the if x > y: .... elif y > x: ... else: .... construct. It makes more sense semantically and also only executes one of the blocks each time not 3. Commented Nov 5, 2013 at 21:14

3 Answers 3

4

You have two problems:

  1. You need to use == for comparison tests, not = (which is for variable assignment).
  2. if needs to be lowercase. Remember that Python is case-sensitive.

However, you should be using elif and else here since no two of those expressions could ever be True at the same time:

Swansea=2

Liverpool=2

if Swansea < Liverpool:
    print("Swansea beat Liverpool")

elif Swansea > Liverpool:
    print("Liverpool beat Swansea")

else:
    print("Swansea and Liverpool drew")

Though using three separate if's won't generate an error, using elif and else is much better for two reasons:

  1. Code clarity: It is a lot easier to see that only one of the expressions will ever be True.
  2. Code efficiency: Evaluation will stop as soon as an expression is True. Your current code however will always evaluate all three expressions, even if the first or the second is True.
Sign up to request clarification or add additional context in comments.

Comments

3

You are probably getting the syntax error because of all the If they need to be lower case if. Also the equality operator is == not =

if Swansea == Liverpool: 
     print("Swansea and Liverpool drew")

Comments

1

Python is case sensitive so probably If is raising invalid syntax. Should be if.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.