Python中定义中的缩进错误

Python中定义中的缩进错误,python,indentation,Python,Indentation,我是python新手,不知道为什么会出现这种缩进错误问题 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "test1.py", line 27 occ_profile = get_occ_profile(daytype) ^ IndentationError: expected an indented block 由于您的

我是python新手,不知道为什么会出现这种缩进错误问题

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test1.py", line 27
    occ_profile = get_occ_profile(daytype)
              ^
IndentationError: expected an indented block

由于您的格式不正确,我只能假设您没有在
def create_profiles(n,month,daytype)
行之后缩进代码块
在python中定义的函数中应该包含的所有内容都需要在函数的
后面缩进四个空格

您的代码应该如下所示:

import numpy

def create_profiles(n,month,daytype):

    if daytype in ['weekday','weekend']:
        pass
    else:
        print 'error in daytype'
        return 0
    if month in range(1,13):
        pass
    else:
        print 'error in month'
        return 0

    no_its = n
    idstring = str(no_its) + 'x_' + 'month-' + str(month) + '_' + 'daytype-' + str(daytype)
    occ_profile_for_file = numpy.zeros([no_its,144])

    for i in range (0,no_its):

        occ_profile = get_occ_profile(daytype)
        occ_profile_for_file[i][:] = occ_profile        

    Occfile = file('Occfile_'+idstring+'.dat', 'a')
    numpy.savetxt('Occfile_'+idstring+'.dat',occ_profile_for_file,fmt="%d", delimiter='\t')
    Occfile.close()

你应该考虑阅读代码格式化和最佳实践,因为这会使你的代码在很长的一段时间内更加可读。

你有<代码>缩进错误。因为这条线没有正确的缩进。检查第行之前的空格数。也许您有一个
选项卡,而不是空格,这也会导致错误。使用autopep8,它将为您处理所有这些格式问题。啊,有人修好了。但答案的实质仍然存在;查看pep8,并在
之后缩进4个空格,谢谢。我会看看政治公众人物指南。这些空间和格式问题从一开始就是一个难题。谢谢我仔细检查了代码,函数或for循环之后的所有内容都缩进了4个空格。我刚刚注意到在
Occfile.close
之后缺少一个()。(我在回答中进行了编辑)。您是否按照上面评论中的建议尝试过AutoEP?因为当我运行上述代码时,根本没有缩进错误:/谢谢问题解决了!是的,我试过autopep8!非常感谢你
import numpy

def create_profiles(n,month,daytype):

    if daytype in ['weekday','weekend']:
        pass
    else:
        print 'error in daytype'
        return 0
    if month in range(1,13):
        pass
    else:
        print 'error in month'
        return 0

    no_its = n
    idstring = str(no_its) + 'x_' + 'month-' + str(month) + '_' + 'daytype-' + str(daytype)
    occ_profile_for_file = numpy.zeros([no_its,144])

    for i in range (0,no_its):

        occ_profile = get_occ_profile(daytype)
        occ_profile_for_file[i][:] = occ_profile        

    Occfile = file('Occfile_'+idstring+'.dat', 'a')
    numpy.savetxt('Occfile_'+idstring+'.dat',occ_profile_for_file,fmt="%d", delimiter='\t')
    Occfile.close()