本文介绍了缩进错误:预期缩进的块python 3.6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手.我想返回字符串"shut down"如果s==yes.请告诉我以下代码中可能出了什么问题.

I am a newbie in python. I wanted to return string "shutting down" if s==yes. Pls tell me what could be wrong in the below code.

def shut_down(s):
... if s == "yes":
  File "<stdin>", line 2
    if s == "yes":
     ^
IndentationError: expected an indented block

推荐答案

tl; dr尝试在第二行的开头添加4个空格,如下所示:

tl;dr try to add 4 spaces to the start of your second line like this:

def shut_down(s):
    if s == "yes"

建议在Python中使用4个空格进行缩进,制表法或其他数量的空格可能会起作用,但也众所周知有时会引起麻烦.有关更多信息,请参见 PEP8 .

It is recommended to use 4 spaces for indentation in Python, tabulation or a different number of spaces may work, but it is also known to cause trouble at times. more on that can be read on PEP8.

我不确定您如何尝试缩进第二行,但要注意的是,大多数现代Python IDE会自动将制表符替换为4个空格,如果您习惯使用制表符,这会非常好(I亲自使用PyCharm,但IDLE也会这样做.

I'm not exactly sure how you tried to indent your second line, but it is important to note that most modern Python IDEs will automatically replace your tabs with 4 spaces, which is great if you're used to tabbing (I personally use PyCharm, but IDLE does that as well).

这篇关于缩进错误:预期缩进的块python 3.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:54