首页 > 编程知识 正文

Python 中的缩进错误

时间:2023-11-26 13:13:14 阅读:309422 作者:FDEB

在理解 python 中的缩进问题之前,让我们看一下关于 python 编程语言的简介。

python 区别于其他编程语言的地方是什么?

  • Python 是一种解释的高级面向对象编程语言,具有动态语义。
  • 它有一个非常简单的语法,,并且与其他编程语言相比非常容易实现。
  • 使用 python,程序员可以专注于逻辑构建,而不是记住语法或面临在编码中实现逻辑的问题。
  • 它是一种动态而智能的高级语言,在各种用户中使用最为广泛。
  • 我们将 python 编程语言语法与其他编程语言进行比较,如 C、C++、Java、以及更多遵循特定语法的语言。对于每个代码块,都有左大括号和右大括号。
  • 但是在 python 中,我们将遵循适当的缩进规则。
  • 缩进是指在开始一个特定的代码块时,空格的适当参与。
  • 它不太复杂,也不太注重语法。
  • 它比其他编程语言更快更有效。
  • Python 代码可以很容易地实现和编写,并且很容易执行。

现在让我们讨论 python 中缩进的概念。

Python 编程语言中的缩进是什么?

  • 缩进是指在 python 编程语言中的任何语句、方法和代码块开始时实现适当的空格和制表符。
  • 由于这些缩进,我们可以很容易地识别任何条件循环、函数、If-else语句等的起点和终点。
  • 假设我们观察其他编程语言,如 C、C++和 Java。在这种情况下,使用小括号和大括号以及分号来指定特定的代码块和标识特定的语句。
  • 但是在 python 中,我们不会关心在语法中使用分号和不同的大括号。
  • c 语言中没有空格的重要性,其他编程语言或者编译器会忽略所有的空格和注释,在词法阶段本身,在开始阶段;因此,它不会产生语法错误。
  • 所有空白空间的作用都很重要;如果一个代码块通过应用空格开始,它必须在相同的缩进处结束。
  • 所有语句必须具有相同数量的空格或制表符,以便屏幕左侧的距离属于同一代码块。如果一个块必须嵌套得更深,它将进一步向右缩进。
  • 假设我们用 python 编程语言观察。在这种情况下,没有主函数的概念,因为我们将其与其他编程语言进行了比较。它们都有一个主函数的概念,在其他函数之后执行。如果主函数调用另一个函数,指针将指向执行其他函数并再次返回主函数。
  • 由于缩进,决定了最初缩进的块是主函数的主体。如果有其他函数,那么初始缩进块将调用该函数。附加函数的缩进也不同于主体,这样编译器可以区分主函数和派生函数的块。

Python 编程语言中使用的一些基本缩进规则

  • 在开始和结束任何代码块时,必须维护特定代码块的适当缩进。
  • 程序员可以通过使用反斜杠将缩进分成多行。
  • 如果任何没有保持适当缩进的块,python 编译将抛出缩进错误。
  • 如果代码太大或包含大量代码行,遵循缩进规则的一致性对程序员来说是非常混乱的。
  • 但是缩进可以帮助我们使用简单的语法来实现代码,而不是像在其他编程语言中那样,为每个代码块应用括号。
  • 不仅对我们来说很容易,而且节省了我们的时间,减少了代码的行数;因此,最终速度会提高,执行速度会降低。
  • 特定代码块的空白数量已经固定。如果我们使用嵌套的条件语句,那么根据 sub sub 语句的规则,空格的数量会增加。

Python 中缩进错误是如何出现的?

如果我们根据某种逻辑编写某一行代码以获得期望的结果,但同时,我们遵循上述缩进规则。编译器将生成缩进错误。

让我们借助一个例子来理解-

在上图中,如果我们观察到提到 python 如何识别每个代码块。

正如我们将在这个图中注意到的,每个块的缩进是从语句的左上角到开始的空格的计算。每当特定代码块开始时,它将跟随缩进。一开始,无论何时区块开始,直到区块结束,它都会保持适当的空格和制表符。如果在这种情况下,该特定块内部包含嵌套块,它将为其分配空白,直到该嵌套块结束。

这样,在 python 编程语言中,块的缩进匹配。

每当特定块没有结束,或者为开始代码块分配的空白数不同时,就会出现缩进错误。在块结束的同时,最终会出现缩进错误。

上图描述-

  • 在上图中,将执行语句一。然后,指针将指向“if语句”并检查条件;如果条件为真,它将进入块并进入嵌套的 if-else 块。
  • 同样在嵌套的 if语句中,它将检查条件是否为真,然后执行下一条语句。
  • 这样,语句的执行就会进行下去,现在;如果我们以块失败为例,它将不会进入嵌套的 if-else 块。它直接从所有块中退出,最后的语句将执行,它与块 1,初始块相匹配。

现在让我们看看有缩进错误的图-

上面提到的图显然有缩进错误,因为块 2 缩进与起点和终点空格不匹配。块 2 的开头将包含 0 个空格,但是当它结束时,它包含 2 个空格,这将产生一个错误。

现在出现了一个疑问,如果块 3 下面的块 2 可以作为块 4 的嵌套块,那么它不会产生缩进错误,那么这个疑问的答案是否定的,因为块 4;与块 3 相比,块 3 下方的嵌套块将具有更多空白。毕竟,它嵌套在块 3 中。第二个原因是 python 解释器会找到块 2 的结尾,但它无法从块 2 中检测到匹配的空格;因此,它会产生缩进错误。

如何修复 python 编程语言中的缩进错误?

修复缩进错误时,必须记住以下几点:

正如我们前面所看到的,缩进错误的发生仅仅是由于不匹配的空格和制表符;因此,我们需要修复所有的空格和标签。

  • 首先,您需要找到特定代码行中的错误。
  • 之后,检查那一行中的空格数,并检测那一行所在的代码块。
  • 通过计算在该块的开始和结束处使用的空白数,将该块的缩进与该块的结束和开始点匹配。
  • 尽管如此,如果错误没有被解决,通过使用计算空格和制表符的相同方法,将每个代码块的缩进与另一个相应的代码块匹配。
  • 此外,您还可以借助正在使用的编辑器来检查所使用的空格和制表符的数量,并且通过找出所使用的不必要的空格,您可以很容易地从中检测出来。

Python 中缩进的优势

  • 缩进在 python 中用于表示某个代码块,但在其他编程语言中,它们指的是各种括号。由于缩进,代码看起来更高效,结构更美观。
  • python 编程语言中使用的缩进规则非常简单;甚至程序员也希望他们的代码可读。
  • 此外,缩进通过漂亮的结构提高了代码的效率和可读性。

Python 中缩进的缺点

  • 由于在缩进中使用了空格,当有很多行代码时,有时修复缩进错误是一项非常困难的任务。
  • 各种流行的编程语言,如 C、C++、Java,都使用大括号进行缩进,因此来自发达世界另一端的任何人都很难适应使用空格进行缩进的想法。

Python 缩进错误示例:

例 1:


# Python program to find the maximum out of two numbers:
def max(x,y):  # max function will return the maximum among the two numbers
  if(x>y):
  return x
  else:
    return y
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
print("Finding the Maximum out of a:", a ,"and b:", b)
c=max(a,b)     # calling the max function
print(c,"is maximum")           # printing the result

上述程序的输出:

IndentationError: expected an indented block 

例 2:


# Python program to find the maximum out of three numbers:
def max(x,y,z):  # max function will return the maximum among the three numbers
if(x>y):
   if(x>z):
     return x
   else:                                                # Nested if else block
     return z
  else:
    if(y>z):
      return y
    else:
      return z   
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Finding the Maximum out of a:", a ,"and b:", b, "and c:",c)
d=max(a,b,c)     # calling the max function
print(d,"is maximum")           # printing the result

上述程序的输出:

IndentationError: expected an indented block    

例 3:


# Python program to calculate the grade from the given result of a student:
# Grade function is used to obtain the corresponding grade from the given result
def grade(x):                  
  if(x>=95):                      
    return 'A+'
  elif(90<=x<95):
    return 'A'
  elif(85<=x<90):
  return 'B+'
  elif(80<=x<85):
    return 'B'
  elif(70<=x<80):
    return 'B-'
  elif(60<=x<70):
    return 'C'
  elif(40<=x<60):
    return 'D'
  else:
    return 'FAIL'
a=int(input("Enter total marks of a student: "))
print("Finding the grade obtained by a student from the given marks: ")
g=grade(a)
# Printing the result
print("Result of a student having marks:",a,"obtained grade is:",g)  

上述程序的输出:

 IndentationError: expected an indented block   

现在修复 python 程序上面例子中的缩进错误:

例 1:


# Python program to find the maximum out of two numbers:
def max(x,y):  # max function will return the maximum among the two numbers
  if(x>y):
    return x
  else:
    return y
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
print("Finding the Maximum out of a:", a ,"and b:", b)
c=max(a,b)     # calling the max function
print(c,"is maximum")           # printing the result

上述程序的输出:

例 2:


# Python program to find the maximum out of three numbers:
def max(x,y,z):  # max function will return the maximum among the three numbers
  if(x>y):
   if(x>z):
     return x
   else:                                                # Nested if else block
     return z
  else:
    if(y>z):
      return y
    else:
      return z   
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Finding the Maximum out of a:", a ,"and b:", b, "and c:",c)
d=max(a,b,c)     # calling the max function
print(d,"is maximum")           # printing the result

上述程序的输出:

例 3:


# Python program to calculate the grade from the given result of a student:
# Grade function is used to obtain the corresponding grade from the given result
def grade(x):                  
  if(x>=95):                      
    return 'A+'
  elif(90<=x<95):
    return 'A'
  elif(85<=x<90):
    return 'B+'
  elif(80<=x<85):
    return 'B'
  elif(70<=x<80):
    return 'B-'
  elif(60<=x<70):
    return 'C'
  elif(40<=x<60):
    return 'D'
  else:
    return 'FAIL'
a=int(input("Enter total marks of a student: "))
print("Finding the grade obtained by a student from the given marks: ")
g=grade(a)
# Printing the result
print("Result of a student having marks:",a,"obtained grade is:",g)  

上述程序的输出:


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。