首页 文章

“缩进错误:unindent与任何外部缩进级别都不匹配”[关闭]

提问于
浏览
0

我有错误:“缩进错误:unindent不匹配任何外部缩进级别”在第10行的文件的这一部分中抛出 . 我已经尝试重新缩进所有它但我找不到原因为什么它在这里抛出这个错误:

import os
import time


if not os.path.exists("Desktop/ServerNotify/ServerNotifier.txt"):
    os.makedirs("Desktop/ServerNotify/ServerNotifier.txt")

def WriteAddressToFile():
    theFile = open("ServerList.txt")

  for line in theFile:
    if line == server:
      theFile.close()
    else:
      theFile.close()

      theFile = open("ServerList.txt", "a")
      theFile.write(server + "\n")
      theFile.close()

1 回答

  • 2

    您的for-block需要缩进两个空格:

    def WriteAddressToFile():
        theFile = open("ServerList.txt")
    
        for line in theFile:
          if line == server:
            theFile.close()
          else:
            theFile.close()
    
            theFile = open("ServerList.txt", "a")
            theFile.write(server + "\n")
            theFile.close()
    

    请注意它现在如何与此行对齐:

    theFile = open("ServerList.txt")
    

    另外,只是有点唠叨,缩进的Python标准是4个空格 . :)

相关问题