IndentationError in Python in definition

IndentationError in Python in definiation

我是 python 的新手,不确定为什么会遇到这个 IndentationError 问题

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

有什么建议吗?

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

由于您不幸的格式设置,我只能假设您没有在 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()

您真的应该考虑阅读 PEP Guidelines 关于代码格式和最佳实践的文章,因为它使您的代码在很长一段时间内更具可读性。