简体   繁体   中英

unexplained python indentation error

I'm coding in python for homework. I wrote a couple of functions, everything works great. I tried to add a third function, and python gives me the message "expected an indented block". I know there's a problem mixing tabs and spaces. I tried them both and it didn't make a difference. Tried changing tab spacing, rewriting the entire code on a different PC. I am clueless... what could be the problem?

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):

You might still mixing tabs and spaces, don't do that.

Run python -tt yourscript.py to detect where the indentation has become inconsistent. Adjust your editor to only use spaces (expand tabs to spaces, use spaces for indentation, etc.).

Note that you do need to specify a body for the new function, otherwise you'll get that same error:

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

    ^
IndentationError: expected an indented block

Is there anything after the last line in your example?

def check_datagram(datagram,src_comp,dst_app):

If not: Python requires that blocks of code not be "empty." I would change it to:

def check_datagram(datagram,src_comp,dst_app):
    pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM