简体   繁体   English

Python elif 适用于 IDLE,但不适用于 Visual Studio Code

[英]Python elif works in IDLE but not Visual Studio Code

I'm a Python newbie, currently working on my first project.我是 Python 新手,目前正在做我的第一个项目。

My elif statements seem to work in IDLE but not VSC我的 elif 语句似乎适用于 IDLE 但不适用于 VSC

To demonstrate, I have a very simple if statement:为了演示,我有一个非常简单的 if 语句:

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.当我将此代码提交给 IDLE 时,它没有问题。 However, when I copy paste the exact same code into VSC and run in Python Terminal I get the following errors:但是,当我将完全相同的代码复制粘贴到 VSC 并在 Python 终端中运行时,出现以下错误:

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.如果我删除 elif 部分它工作正常,所以我觉得我必须缺少一些基本的东西。

Any help would be greatly apprecaited!任何帮助将不胜感激!

edit: Increasingly odd behaviour leads me to believe that this is somehow a Visual Studio issue:编辑:越来越奇怪的行为让我相信这在某种程度上是一个 Visual Studio 问题:

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在“Python 交互式窗口”中运行代码 = 成功全新启动 VSC 并使用“在终端中运行 python 文件” = 成功“在终端中运行选择/行” = 在终端已运行后运行“在终端中运行 python 文件”时遇到上述错误= 上面遇到的错误

edit: People are rightly pointing out that that it looks like VSC is saying that there is an extra line being added.编辑:人们正确地指出,VSC 似乎在说要添加额外的行。 I don't think that is the case: here's a screenshot of the code in VSC我不认为是这种情况:这是 VSC 中代码的屏幕截图

描述

Based on the terminal snippet and screenshot, what VSC is actually running is equivalent to this:根据终端片段和屏幕截图,VSC 实际运行的内容相当于:

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!') .问题是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.它让 Python 认为 if 语句已经全部完成,所以当它看到elifelif所有内容时,它就会出错。

The source of the problem isn't clear though.问题的根源尚不清楚。

Notice...注意...

 >>> elif

You've started a new statement and ended the if statement你开始了一个新的语句并结束了 if 语句

elif <conditional> on its own is invalid, and each following line therefore is being interpreted on its own. elif <conditional>本身是无效的,因此下面的每一行都被单独解释。

I suggest using IPython rather than the regular python REPL, or use JupyterLab我建议使用 IPython 而不是常规的 Python REPL,或者使用 JupyterLab

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码在python IDLE中工作但在visual studio中没有? - Code working in python IDLE but not in visual studio? Python计划脚本在IDLE中运行,但不在Visual Studio Code中运行 - Python schedule script running in IDLE but not in Visual Studio Code Python 在 Visual Studio Code 中找不到模块,但其他 IDLE 找到了它 - Python module not found in Visual Studio Code but other IDLE found it 有没有办法在 visual studio 代码中使用 python IDLE Shell? - Is there any way to use the python IDLE Shell in visual studio code? Python 代码在 IDLE 中有效,但在 VS Code 中无效 - Python Code works in IDLE but not in VS Code 为什么我的 Python 代码在 Visual Studio Code 中没有正确运行,但在 IDLE 中运行良好? - Why is my Python code not running correctly in Visual Studio Code, but runs fine in IDLE? Python 代码适用于 JupyterNotebook(本地)但不适用于 Visual Studio Code(ssh) - Python code works on JupyterNotebook (local) but not on Visual Studio Code (ssh) Python 代码在 Visual Studio Code 中显示 EOF 错误,但在 Visual Studio 中工作正常 - Python code showing EOF error in Visual Studio Code but works fine in Visual Studio Visual Studio 中的 PyGame - 加载图像会出错,但是当我将相同的代码粘贴到 IDLE 时,它运行良好。 任何修复? - PyGame in Visual Studio - Loading images gives an error, but when I paste the same code to IDLE, it works perfect. Any fixes? 与Visual Studio Code的调试控制台配合使用的Python进度指示器? - Progress indicator for Python that works with Visual Studio Code's debug console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM