2012-12-11 83 views
0

我在python中編寫作業。我寫了幾個函數,一切都很好。我試圖添加第三個函數,python給我的消息「期望一個縮進塊」。我知道混合標籤和空格存在問題。我嘗試了它們,但沒有什麼區別。嘗試更改制表符間距,在不同的PC上重寫整個代碼。我無能爲力......可能是什麼問題?不明原因的python縮進錯誤

def xor_bytes(byte1, byte2): 
    xor = "" 
    for i in range(len(byte1)): 
     if byte1[i] == byte2[i]: 
      xor = xor + "0" 
     else: 
      xor = xor + "1" 
    return xor 

def verify_checksum(datagram): 
    checksum = '00000000' 
    total = False 
    for i in range((len(datagram)/8)-1): 
     checksum = xor_bytes(checksum,datagram[8*(i):8*(i+1)]) 
     if checksum == datagram[len(datagram)-8 : len(datagram)]: 
      total = True 
    return total 
def check_datagram(datagram,src_comp,dst_app): 
+2

您是否爲第三個功能添加了正文? –

回答

1

你可能仍然混合標籤和空格,不這樣做。

運行python -tt yourscript.py檢測其中的縮進變得不一致。調整編輯器以僅使用空格(將選項卡擴展到空格,使用空格縮進等)。

注意,你需要指定一個新功能,否則你會得到同樣的錯誤:

>>> def foo(bar): 
... 
    File "<stdin>", line 2 

    ^
IndentationError: expected an indented block 
0

有沒有在您的示例中的最後一行後什麼?

def check_datagram(datagram,src_comp,dst_app): 

如果不是:Python要求代碼塊不是「空的」。我會將其更改爲:

def check_datagram(datagram,src_comp,dst_app): 
    pass