Why does this syntax error occur?

-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:/ $ | 
    
asked by anonymous 22.06.2018 / 21:45

1 answer

5

You have several errors:

  • Indentation is incorrect
  • missing : after else
  • Incidentally, the else should be elif and the : should come after name != 'Alice'
  • line break missing after else

The correct one would be:

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

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

While looking at the script, you do not even need != 'Alice' , since the first if and elif would be redundant in this case , then that's enough:

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

if name == 'Alice':
    print ('oi, alice.')
else:
    print ('Tchau.')
    
22.06.2018 / 21:53