Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "expected an indented block" when I try to run my Python script? [closed]

I have an error which says "expected an indented block" Could you please guide me on how to deal with this error. Thank you:)

Code example:

for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch) 
like image 497
boddhisattva Avatar asked Feb 13 '10 14:02

boddhisattva


People also ask

How to resolve the indentationerror expected an indented block in Python?

If the code is not indented correctly, you will face this IndentationError: expected an indented block. In Python, Indentation is part of the syntax. Let’s see how to resolve the Indentation error. Python expected an indented block. To resolve expected an indented block in Python, correct the indentation of each block.

Does Python need indentation?

Programming languages like C and JavaScript do not require particular indentation. This is because they use curly braces to denote the structure of code blocks. Python does not use curly braces or a similar indicator. The language depends on indentation to determine the structure of a program.

What are the causes of indentationerror?

Some causes of indentation error are: When we forget to indent the statements of a user-defined function. The error message IndentationError: expected an indented block would seem to indicate that you have a spaces error or indentation error.

Why is my Python variable declaration not indenting?

This causes an error because Python functions expect to have at least one line of code indented below where they are declared. To solve this error, we need to indent our variable declaration: Our code finds all of the bagels in our “lunch_menu” list and adds them to the “bagels” list. Our code then prints out the list of bagels to the console.


1 Answers

Editing answer to match the code example.

for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)  

is just not valid Python.

First, readability count. Your code is hard to read and so, is hard to debug. What's "ch" and "f" ? What's more, you can do one liner in Python but it's not recommended, so put the for in a separate line. Then indent.

for chunk in file: 
    ( translatedToken = english_hindi_dict[chunk] ) if (chunk in english_hindi_dict) else (translatedToken = chunk)

Now we can see what's wrong. You make variable assignments in a conditional statement. This is not allowed in Python. I'm guessing you have a C/C++ background and are used to do that. In Python you can't, to prevent you from writing obfuscated code. So you end up with:

for chunk in file: 
    translatedToken = english_hindi_dict[chunk] if chunk in english_hindi_dict else chunk

This piece of code should work, provided you use Python 2.5+. But the ternary operator is not available in older Python version yet. Let's make it a bit friendlier:

for chunk in file: 
    translatedToken = chunk
    if chunk in english_hindi_dict:
        translatedToken = english_hindi_dict[chunk]

You may argue that it's longer to write, and you'd be right. But you spend more time reading code than writing it, so it make sense to make it easy to read. Or course, once you have the Python grip, you will try to make it work in a more pythonic way. Ever heard of EAFTP?

for chunk in file: 
    try:
        translatedToken = english_hindi_dict[chunk]
    except KeyError:
        translatedToken = chunk

But Python is full of surprises, and you'll learn that most of these classic use cases have been already taken care of. The standard library often provides an elegant and short yet readable solution:

for chunk in file: 
    translatedToken = english_hindi_dict.get(chunk, chunk)

As a conclusion: don't try to write Python as you wrote C, or Java as you would write Perl. Other tool, other style.


To fix this problem, fire your editor "search and replace" feature and make a huge "replace all" to change all the tabs by 4 spaces, or the contrary. Then indent all your blocks, and finally align all the instructions in the same block.

Funny that didn't appear before on SO. After all, it's true it's not that obvious.

In Python, you separate blocks using spaces or tabs, not "{".

So any time you go down a block (a function, a loop, a class, etc), you have to indent your code. This is not just good practice, this is mandatory. Your program will crash if you don't.

Now, most of the time, you get this error because you did indent, but used tabs and spaces. In a Python program, you should use either tabs or spaces, but never both in the same files.

E.G:

if (age > 18)
{
    printf("You can vote")
}

Becomes:

if age > 18:
    print("You can vote")

In most languages, you could do:

if (age > 18)
{
printf("You can vote")
}

In Python you can't:

if age > 18:
print("You can vote")

raises an exception. What's more, you must align all the instruction of the same block, so:

if age > 18:
    print("You can vote")
    print("How cool is that ?")

Is fine, but:

if age > 18:
    print("You can vote")
   print("How cool is that ?")

raises an exception.

Eventually, you can't mix tab and spaces in the same block. So:

if age > 18:
    print("You can vote")
    print("How cool is that ?")

looks good, but will raises an exception. To avoid this problem, just stick to tabs or spaces. The PEP8, the text one most use as a reference for coding style recommend using 4 spaces.

Most editors have a global "search and replace" feature that let you fix any problem you can have with that. Some like Geany or Ulipad even have a "replace all tabs with spaces" feature.

like image 129
e-satis Avatar answered Sep 25 '22 02:09

e-satis