2015-10-09 18 views
0

我是新來的蟒蛇,不知道爲什麼我有這個IndentationError問題IndentationError在Python中definiation

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 
+2

您有'IndentationError',因爲此行沒有正確縮進。在行之前檢查空格的數量。也許你有一個'tab'而不是空格,這也會導致錯誤。 – Psytho

+0

使用autopep8,它會爲你處理所有這些格式問題。 https://pypi.python.org/pypi/autopep8/ –

回答

0

由於您不幸格式化,我只能假設你沒有行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,因爲它使你的代碼更易讀由伸得很長。

+0

啊,有人修好了。答案的本質仍然存在;檢查pep8出來,然後在':' – nlsdfnbch

+0

之後用4個空格縮進。謝謝。我將看看PEP指南。這些空間和格式問題在起點上確實很痛苦。謝謝! –

+0

我仔細檢查了代碼,並且函數或for循環之後的所有內容都縮進了4個空格。 –

相關問題