Python中的IndentationError在definiation中

2024-09-28 20:57:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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

Tags: noinnumpyforprofilefileitsstr
1条回答
网友
1楼 · 发布于 2024-09-28 20:57:26

由于您不幸的格式设置,我只能假设您没有在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关于代码格式和最佳实践的文章,因为它可以让您的代码在很长一段时间内更具可读性。你知道吗

相关问题 更多 >