[Visual Studio Code在执行if语句时总是给我缩进或语法错误?

问题描述 投票:0回答:1
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.")

我正在尝试修改一些初学者级别的Python,并且一直在我的脚本中使用Visual Studio Code(在Mac上)。每当我尝试运行诸如上述if语句之类的代码块时,都会得到诸如"SyntaxError: invalid syntax""IndentationError: unexpected indent"之类的错误代码。尝试再次进行缩进,并在Sublime中检查了我的缩进,这很好。该代码也可以在我的终端上运行。

我认为我的VSC出现了问题,它运行的是Python 3.8,这是我在Mac终端中使用的,可以很好地运行代码,但是在使用其终端时表现得很时髦。

这很困扰我,因为这意味着我以后需要做更复杂的事情时可能需要切换编辑器。

任何帮助将不胜感激。

python macos visual-studio-code syntax indentation
1个回答
0
投票

如果您逐行发送此代码,则可能会遇到代码与REPL期望的不一致的情况。

我还要说,如果您想使其更加灵活,可以稍微简化一下代码:

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."))
© www.soinside.com 2019 - 2024. All rights reserved.