Reputation: 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
Upvotes: -1
Views: 6885
Reputation:
You have two problems:
==
for comparison tests, not =
(which is for variable assignment).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:
True
.True
. Your current code however will always evaluate all three expressions, even if the first or the second is True
.Upvotes: 4
Reputation: 8685
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")
Upvotes: 3
Reputation: 29804
Python is case sensitive so probably If
is raising invalid syntax. Should be if
.
Upvotes: 1