4

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
Hrvoje T
  • 3,365
  • 4
  • 28
  • 41

2 Answers2

3

You are mixing tabs and spaces

before if you have used one spaces and two tabs

In python you should not be mixing tabs and space you should use either tab or space

You can find that using python -tt script.py

Most of the python developer prefer space to tab

The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • I used Atom but the code was writen in Sublime. Now I converted all the code from tabs to spaces. It works now. Thanks. – Hrvoje T Jul 01 '15 at 10:57
1

Python generally requires you to have the same level of indents in your code (generally multiples of 4 spaces, which is basically the same as a single tab).

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
Nishant Roy
  • 1,043
  • 4
  • 16
  • 35
  • 1
    Four spaces are not the same as a single tab. How many spaces a tab counts for depends entirely on conventions and editor settings. A tab may even be worth varying amounts of spaces, depending on the column it starts on. – mkrieger1 Jul 01 '15 at 10:44
  • Thanks for pointing that out @mkrieger1 you're right. But many Python IDEs by default set a tab as 4 spaces. But correct, it doesn't have to be 4 spaces. Also, in several IDEs, if you're in a column between multiples of 4, tab takes you to the next column that is a multiple of 4. – Nishant Roy Jul 01 '15 at 10:48