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.