[英]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.