编译Python代码时unindent不匹配任何外部缩进级别错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编译Python代码时unindent不匹配任何外部缩进级别错误相关的知识,希望对你有一定的参考价值。

错误:

unindent does not match any outer indentation level

代码:

def confirmation(self):
    if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
        print("Checkout Failed!")

     if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
        print("Checkout Succesful!")

我该如何解决该错误?

答案

此错误消息...

unindent does not match any outer indentation level

...表示代码块中的缩进存在问题。

[在缩进Python代码时,混合使用TabSpace字符时,通常会观察到此错误。


分析

当我复制您的代码块并执行时:

indentation_problem

我遇到相同的错误:

C:UsersusernameDesktopdirectoryPyPrograms>class_in_python.py
  File "C:UsersSoma BhattacharjeeDesktopDebanjanPyProgramsclass_in_python.py", line 26
    def confirmation(self):
              ^
IndentationError: unindent does not match any outer indentation level

一旦我删除了所有空白步伐和新的换行符,并为下面的代码行再次分行,默认缩进似乎有所不同:

     if WebDriverWait(self.driver, 1000).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
        print("Checkout Succesful!")

indentation_difference


解决方案

您需要修正以下几行的缩进:

  • EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
  • EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):

您可以在Is there something issue in indentation, while using Anaconda's Spyder中找到相关的详细讨论

另一答案

第二个'if'的缩进与第一个不匹配,它在一个额外的空格中。在python中,您的缩进必须一致]

基本上,您可以缩进第二个'if'与第一个'if'甚至第一个'print'都是偶数(在这种情况下,第二个'if'将在第一个'if'之内')。第二个'if'的当前位置与上述任何先前级别都不匹配,因此python引发错误。

另一答案

[.until(之后,您正在换一行,Python正在考虑将其换为新代码行,从而引发错误。另外,第二个if和print前面还有多余的空格。

def confirmation(self):
    if WebDriverWait(self.driver, 1000).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
            print("Checkout Failed!")

    if WebDriverWait(self.driver, 1000).until(
      EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
          print("Checkout Succesful!")

以上是关于编译Python代码时unindent不匹配任何外部缩进级别错误的主要内容,如果未能解决你的问题,请参考以下文章

IndentationError:unindent 不匹配任何外部缩进级别我啥都不懂[关闭]

IndentationError: unindent does not match any outer indentation level

python中出现IndentationError:unindent does not match

python中出现 IndentationError:unindent does not match any outer indentation level

python中出现IndentationError:unindent does not match any outer indentation level错误

构建代码期间的 Python 版本不匹配