【发布时间】:2020-07-27 17:07:25
【问题描述】:
# Python 3 implementation of the approach
# Linearly search x in arr[]. If x is present
# then return the index, otherwise return -1
def search(arr, n, x):
for i in range(n):
if arr[i] == x:
return i
return -1
# Driver Code
arr = [1, 10, 30, 15]
x = 30
n = len(arr)
print(x, "is present at index",search(arr, n, x))
如何解决上述 Python 代码的错误 Error : IndentationError: expected an indented block 此代码旨在检查并返回数组值。
【问题讨论】:
-
Python 使用缩进(每行开头的空格)来了解哪些代码组合在一起。因此,
for i in range(n)需要比def search缩进更多。此外,x = 30不允许比arr = ...缩进更多 -
你应该学习python语法的基础(以及缩进,这是python的基础)。在你的情况下。
def从第 2 列开始(应该从第 1 列开始),还有驱动程序代码:它都应该从第 1 列开始 -
@Ajay 我已经更新了你的代码并解决了缩进错误
-
查看this doc 了解 Python 和缩进
-
@UmairMubeen 谢谢,我现在可以编译了。