user3287808
user3287808

Reputation: 39

Python gives me an Indentation Error : expected an indented block for a for loop

This is my code :

if (choice == 1) :
        i = 0
        username = raw_input("Enter your User Name : ")
        with open("users.txt", "r") as ins :
            for line in ins :
                i += 1
                if (username == line.rstrip()) :
                    j = 0
                    password = raw_input("Enter your Password : ")
                    with open("passwords.txt", "r" as sth) :
                        for line1 in sth :
                            j += 1
                            if (i == j) :
                                if (password == line1.rstrip()) :
                                    usrName = username
                                    break
                    break
            break

I'm trying to create a program which accepts a username and a password and compares them with their equivalents in a .txt file. However, python gives me the following error :

test@test-PC:~/Desktop/test$ python main.py
File "main.py", line 31
for line in ins :
  ^
IndentationError: expected an indented block

I can't figure out where I have not indented correctly. Can anyone please tell me what causes this problem and suggest a solution ?

Upvotes: 0

Views: 2878

Answers (2)

Anshul Goyal
Anshul Goyal

Reputation: 76887

You seem to be using a mix of spaces and tabs, each of which is counted as a single indentation and could potentially be counted as wrong indentation, even though it looks fine visually, so just use spaces instead (4 spaces, which is PEP-008 recommendation)

Your last break is indented out of place, it simply doesn't correspond to any for loop. If the code snippet above is all the code there is, I would simply remove it.

More precisely, it is this one:

for line in ins :
|   i += 1
|   if (username == line.rstrip()) :
|       j = 0
|       password = raw_input("Enter your Password : ")
|       with open("passwords.txt", "r" as sth) :
|           for line1 in sth :
|               j += 1
|               if (i == j) :
|                   if (password == line1.rstrip()) :
|                       usrName = username
|                       break
|       break
break # What does it break out of here?

Upvotes: 2

silent_dev
silent_dev

Reputation: 1616

The indentation error usually happens when there is a mix of spaces and tabs together. Also, as @mu mentioned,the break should be in place.

Upvotes: 1

Related Questions