mardi 14 juillet 2020

python if elif statement report syntax error [closed]

I am a beginner of python and confused by if elif statement. It does not work in vscode and I cannot find out what the problem is. Here is my code

a = 1
if a == 1: print('a = 1')
elif a == 2: print('a = 2')
else: print('Unknown a')

The error message I got is

a = 1
if a == 1: print('a = 1')  
...
a = 1
elif a == 2: print('a = 2')
File "<stdin>", line 1
elif a == 2: print('a = 2')
   ^
SyntaxError: invalid syntax
 else: print('Unknown a')
File "<stdin>", line 1
else: print('Unknown a')
   ^
SyntaxError: invalid syntax

But the code works if I note the elif part like

a = 1
if a == 1: print('a = 1')
#elif a == 2: print('a = 2')
else: print('Unknown a')

or define a function using this code

def ef(a):
    if a == 1: print('a = 1')
    elif a == 2: print('a = 2')
    else: print('Unknown a')
ef(a = 1)

The IDE I used is VSCode. I appreciate your support.

Aucun commentaire:

Enregistrer un commentaire