Search code examples
pythonindentation

Unexpected indent in Python function


I want that my function returns encoding. User should import it. However, if a user hits enter, the function should return windows-1250 as default encoding.

When I run this code I get an error:

if enc == '': ^ IndentationError: unexpected indent

def encoding_code(prompt):
    """
    Asks for an encoding,
    throws an error if not valid encoding.
    Empty string is by default windows-1250.
    Returns encoding.
    """
    while True:
        enc = raw_input(prompt)
        if enc == '':
            enc = 'windows-1250'

        try:
            tmp = ''.decode(enc) # Just to throw an error if not correct
        except:
            print('Wrong input. Try again.')
            continue
        break
    return enc

Solution

  • You are mixing tabs and spaces.

    Before if, you have used one space and two tabs.

    In Python, you should not be mixing tabs and spaces. You should use either tabs or spaces.

    You can find that using python -tt script.py.

    Most of the Python developers prefer spaces to tabs.