简体   繁体   中英

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!

edit: Increasingly odd behaviour leads me to believe that this is somehow a Visual Studio issue:

Running code in 'Python interactive window" = Successful Fresh Launch of VSC and using 'Run python file in terminal' = Succesful 'Run selection/line in terminal' = Error encountered above Running 'Run python file in terminal' after terminal has already running = error encountered above

edit: People are rightly pointing out that that it looks like VSC is saying that there is an extra line being added. I don't think that is the case: here's a screenshot of the code in VSC

描述

Based on the terminal snippet and screenshot, what VSC is actually running is equivalent to this:

dud = 'You'

if dud == 'You':
    print('You got the dud!')

elif dud == 'Me':
    print('ohhhh, I made myself sad')
else:
    pass

The problem is that second line break after print('You got the dud!') . It's making Python think that the if-statement is all done, so when it sees the elif and everything that follows, it errors.

The source of the problem isn't clear though.

Notice...

 >>> elif

You've started a new statement and ended the if statement

elif <conditional> on its own is invalid, and each following line therefore is being interpreted on its own.

I suggest using IPython rather than the regular python REPL, or use JupyterLab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM