Python给了我一个缩进错误:for循环需要缩进块

Python给了我一个缩进错误:for循环需要缩进块,python,indentation,Python,Indentation,这是我的代码: 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()) :

这是我的代码:

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
我正在尝试创建一个程序,它接受用户名和密码,并将它们与.txt文件中的等效项进行比较。但是,python给了我以下错误:

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

我想不出哪里缩进不正确。谁能告诉我这个问题的原因并提出解决方案

您似乎混合使用了空格和制表符,每个空格和制表符都被视为一个缩进,并且可能被视为错误的缩进,即使它在视觉上看起来很好,所以只需使用空格即可(4个空格,即)

您的最后一个
break
缩进不正确,它根本不对应于任何
for
循环。如果上面的代码片段就是所有的代码,我只想删除它

更准确地说,就是这个:

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?

您似乎混合使用了空格和制表符,每个空格和制表符都算作一个缩进,并且可能算作错误的缩进,即使它在视觉上看起来很好,所以只需使用空格即可(4个空格)

您的最后一个
break
缩进不正确,它根本不对应于任何
for
循环。如果上面的代码片段就是所有的代码,我只想删除它

更准确地说,就是这个:

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?

您似乎混合使用了空格和制表符,每个空格和制表符都算作一个缩进,并且可能算作错误的缩进,即使它在视觉上看起来很好,所以只需使用空格即可(4个空格)

您的最后一个
break
缩进不正确,它根本不对应于任何
for
循环。如果上面的代码片段就是所有的代码,我只想删除它

更准确地说,就是这个:

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?

您似乎混合使用了空格和制表符,每个空格和制表符都算作一个缩进,并且可能算作错误的缩进,即使它在视觉上看起来很好,所以只需使用空格即可(4个空格)

您的最后一个
break
缩进不正确,它根本不对应于任何
for
循环。如果上面的代码片段就是所有的代码,我只想删除它

更准确地说,就是这个:

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?

缩进错误通常发生在空格制表符混合在一起时。
此外,正如@mu所提到的,中断应该在适当的位置。

缩进错误通常发生在空格和制表符混合在一起时。
此外,正如@mu所提到的,中断应该在适当的位置。

缩进错误通常发生在空格和制表符混合在一起时。
此外,正如@mu所提到的,中断应该在适当的位置。

缩进错误通常发生在空格和制表符混合在一起时。
此外,正如@mu所提到的,中断应该到位。

@user3287808如果有帮助,不要忘记:)@user3287808如果有帮助,不要忘记:)@user3287808如果有帮助,不要忘记:)@user3287808如果有帮助,不要忘记:)