Whenever I write elif or else in my code I get a Syntax Error. Why is this?
Question:
I am trying to write code for a simple Rock Paper Scissors and I got stuck here because else or elif don’t work in my code. This is my code
I tried looking up why this didn’t work and I couldn’t find anything on the subject.
Answers:
It seems you are mixing the order how to use if
–elif
–else
.
The following is quoted from
if, elif and else.. priority and chains (I recommend reading it fully):
else
is the catch-all after all the if
and elif
statements were False
. Putting it in the middle would be a syntax error.
To demonstrate how the if
–elif
–else
blocks work:
if False:
print 'first'
elif True:
print 'second'
elif True:
print 'third' "
I am trying to write code for a simple Rock Paper Scissors and I got stuck here because else or elif don’t work in my code. This is my code
I tried looking up why this didn’t work and I couldn’t find anything on the subject.
It seems you are mixing the order how to use if
–elif
–else
.
The following is quoted from
if, elif and else.. priority and chains (I recommend reading it fully):
else
is the catch-all after all theif
andelif
statements wereFalse
. Putting it in the middle would be a syntax error.
To demonstrate how the if
–elif
–else
blocks work:
if False:
print 'first'
elif True:
print 'second'
elif True:
print 'third' "