Why does this syntax error occur?

Asked

Viewed 110 times

-2

Code:

print ('oi,tudo bem?')
name = input ()
if name == 'Alice':
    print ('oi, alice.')
    else name != 'Alice'
    print ('Tchau.')

Error:

e/emulated/0/qpython/vampi.py" && exit 
File "/storage/emulated/0/qpython/vampi.p 
y", line 5 
else name != 'Alice' 
     ^
SyntaxError: invalid syntax 
1 |u0_a266@jflte:/ $ | 

1 answer

5


You’re making a lot of mistakes:

  • The indentation is incorrect
  • foul : after the else
  • besides the else should be elif and the : should come after the name != 'Alice'
  • missing line break after Else

the correct would be:

print ('oi,tudo bem?')
name = input ()

if name == 'Alice':
    print ('oi, alice.')
elif name != 'Alice':
    print ('Tchau.')

Although looking at the script, you don’t even need the != 'Alice', since by the first if the elif would be redundant in this case, then that alone will be enough:

print ('oi,tudo bem?')
name = input ()

if name == 'Alice':
    print ('oi, alice.')
else:
    print ('Tchau.')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.