最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Strange indentation errors when running Python code with Shift+Enter in VS Studio Code - Stack Overflow

programmeradmin1浏览0评论

I recently installed VS Code and Python on a new computer and am running into unusual behavior when trying to run a simple try/except block. Take the following test line of code:

try:
    1 / 0
except:
    print("Caught an error!")

If I highlight the code above and press Shift + Enter to execute, I will receive an indentation error as pictured:

I know this is not an indentation error and I can run this same code on one of my other computers and editors without issue. In fact, I can run similarly indented code without issues such as:

for i in range(3):
    print(i)

I can confirm that I am not mixing up tabs and spaces and can also confirm that both lines of code have the exact same indentation when run in VS Code.

I experienced a similar issue trying to use tkinter to create a grid using a for loop where, despite copying and pasting an example verbatim from W3 schools, I received the same sort of indentation error. What could be causing this?

I recently installed VS Code and Python on a new computer and am running into unusual behavior when trying to run a simple try/except block. Take the following test line of code:

try:
    1 / 0
except:
    print("Caught an error!")

If I highlight the code above and press Shift + Enter to execute, I will receive an indentation error as pictured:

I know this is not an indentation error and I can run this same code on one of my other computers and editors without issue. In fact, I can run similarly indented code without issues such as:

for i in range(3):
    print(i)

I can confirm that I am not mixing up tabs and spaces and can also confirm that both lines of code have the exact same indentation when run in VS Code.

I experienced a similar issue trying to use tkinter to create a grid using a for loop where, despite copying and pasting an example verbatim from W3 schools, I received the same sort of indentation error. What could be causing this?

Share Improve this question edited Mar 5 at 8:21 mkrieger1 23.6k7 gold badges64 silver badges82 bronze badges asked Mar 4 at 22:26 cbkirkcbkirk 31 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 0

This is a known issue with Python 3.13. Use python 3.12 will solve it.

发布评论

评论列表(0)

  1. 暂无评论