简体   繁体   English

Visual Studio Code 在执行 if 语句时不断给我缩进或语法错误?

[英]Visual Studio Code keeps giving me indent or syntax errors when executing if statements?

loc = 'Bank'

if loc == 'Auto Shop':
    print("Cars are cool!")
elif loc == 'Bank':
    print('Money is cool!')
elif loc == 'Store':
    print('Welcome to the store!')
else:
    print("I do not know much.")

I'm trying to revise some beginner level Python and have been using Visual Studio Code (on Mac) for my scripts.我正在尝试修改一些初学者级别的 Python,并且一直在为我的脚本使用 Visual Studio Code(在 Mac 上)。 Whenever I try to run blocks of code such as the if statement above, I get error codes like "SyntaxError: invalid syntax" and "IndentationError: unexpected indent" .每当我尝试运行代码块(例如上面的 if 语句)时,我都会收到错误代码,例如"SyntaxError: invalid syntax""IndentationError: unexpected indent" Tried doing my indentation over again and checked my indentation in Sublime and it was fine.再次尝试缩进并检查我在 Sublime 中的缩进,结果很好。 The code also runs in my terminal.该代码也在我的终端中运行。

I think something is up with my VSC, it's running Python 3.8 which is what I used in my Mac terminal to run the code just fine, but is acting funky when using its terminal.我认为我的 VSC 出了点问题,它运行的是 Python 3.8,这是我在 Mac 终端中用来运行代码的方式,但在使用其终端时表现得很奇怪。

This is bothering me as it means I might need to switch editors for when I do more complicated stuff later.这让我很困扰,因为这意味着我可能需要在以后做更复杂的事情时切换编辑器。

Any help would be well appreciated.任何帮助将不胜感激。

If you're sending this line-by-line then you could get into a situation where your code doesn't line up with what the REPL is expecting.如果您逐行发送此内容,那么您可能会遇到您的代码与 REPL 所期望的不一致的情况。

I'll also say you can simplify your code a bit if you want to make it more flexible:我还要说,如果您想让代码更灵活,您可以稍微简化一下代码:

loc = 'Bank'
messages = {"Auto Shop": "Cars are cool!", "Bank": "Money is cool!", "Store": "Welcome to the store!"}
print(messages.get(loc, "I do not know much."))

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

相关问题 这段代码不断给我错误,我是新来的 - This Code keeps giving me errors, and i am new to classes 在Visual Studio中编写语句时无代码输出 - No code output when writing statements in Visual Studio 为什么它不断返回“theIndentationError:意外缩进”并且当我在终端中放入相同的代码时一切正常? - Why it keeps returning me "theIndentationError: unexpected indent" and when i put the same code in the terminal is everything fine? Python 2.7给了我莫名其妙的“语法错误” - Python 2.7 is giving me inexplicable “syntax errors” 当我尝试导入 pandas 时,我的 Python 控制台不断给我错误 - My Python console keeps giving me errors when I try to import pandas 我写了这个 python 程序,它一直给我错误 - I wrote this python program and it keeps giving me errors Visual Studio Code在Python终端中的代码之间添加行导致语法错误 - Visual Studio Code adding lines between code in Python terminal causing syntax errors 我的代码在elif / else语句中不断显示语法错误 - my code keeps showing syntax error in my elif/else statements 这段代码一直在给我:字符串索引必须是整数 - This code keeps giving me: string indices must be integers 这个简单的代码给了我硬语法错误,除数代码 - this simple code is giving me hard syntax error, divisor code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM