[Tutor] indent error on if/elif/else

Ben Finney ben+python at benfinney.id.au
Thu Sep 10 18:18:09 CEST 2015


richard kappler <richkappler at gmail.com> writes:

> Here's the Traceback:
>
>   File "dataFeedTest.py", line 44
>     else:
>        ^
> IndentationError: expected an indented block

A comment is not a statement. So, your second ‘elif’ block is empty,
then immediately followed by an ‘else’; Python expected an indented
block *of statements*.

Yes, that's not exactly clear from the error message; don't feel bad :-)

> Not a clue why it's doing this. Any help?

If you want to show “I will write something useful here later”, try
writing a named function, and immediately write an empty function that
fits that interface::

    def set_delay_real_time():
        pass

    def read_timestamp(line):
        pass

    def create_offset(timestamp):
        pass

    def send_lin_iaw_time(timestamp, offset):
        pass

    if x == ord('1'):
        delay = 0
    elif x == ord('2'):
        delay = 0.5
    elif x == ord('3'):
        set_delay_real_time()
        timestamp = read_timestamp(line)
        offset = create_offset(timestamp)
        send_lin_iaw_server_time(timestamp, offset)
    else:
        print("bad choice, exiting")
        os.exit()

That way, you express your intent in actual function names, which is
vital to good code. You are exercising the discipline to think about
what function to write at exactly the point where that's most important,
i.e. at the point where the function is used.

-- 
 \       “To have the choice between proprietary software packages, is |
  `\      being able to choose your master. Freedom means not having a |
_o__)                        master.” —Richard M. Stallman, 2007-05-16 |
Ben Finney



More information about the Tutor mailing list