samedi 29 février 2020

Python elif works in IDLE but not Visual Studio Code

I'm a Python newbie, currently working on my first project.

My elif statements seem to work in IDLE but not VSC

To demonstrate, I have a very simple if statement:

dud = 'You'
if dud == 'You':
    print('You got the dud!')
elif dud == 'Me':
    print('ohhhh, I made myself sad')
else:
    pass

When I submit this code to IDLE, it works no problem. However, when I copy paste the exact same code into VSC and run in Python Terminal I get the following errors:

PS C:\Users\William> & C:/Users/William/AppData/Local/Programs/Python/Python38-32/python.exe
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dud = 'You'
>>> 
>>> if dud == 'you':
...     print('You got the dud!')
...
>>> elif dud == 'Me':
  File "<stdin>", line 1
    elif dud == 'Me':
    ^
SyntaxError: invalid syntax
>>>     print('ohhhh, I made myself sad')
  File "<stdin>", line 1
    print('ohhhh, I made myself sad')
    ^
IndentationError: unexpected indent
>>> else:
  File "<stdin>", line 1
    else:
    ^
SyntaxError: invalid syntax
>>>     pass
  File "<stdin>", line 1
    pass
    ^
IndentationError: unexpected indent
>>>

Naturally, I've tried various different types of formatting but I can't get it to work. If I remove the elif section it works fine, so I feel like I must be missing something basic.

Any help would be greatly apprecaited!

Aucun commentaire:

Enregistrer un commentaire