0
votes

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

3
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. - Hunter McMillen

3 Answers

4
votes

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.
3
votes

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")
1
votes

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