[英]Python gives me an Indentation Error : expected an indented block for a for loop
這是我的代碼:
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個空格,這是PEP- 008推薦 )
您的最后一個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所提到的,應該在適當的位置休息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.