TypeError: 'int' object is not callable, despite being a list
TypeError: 'int' object is not callable, despite being a list
我在 Python 中编写线性搜索代码,但似乎遇到了语法问题。
我有一个接收数组的函数,在函数内部我通过 len 函数获得了该数组的长度。问题是,尽管将数组传递给函数,但它会引发错误 'int' object is not callable.
如果有人能告诉我我做错了什么以及为什么会这样,我将不胜感激。
我在下面留下了我的代码。
from random import *
def linear_search(array, value):
for i in range(len(array)): #line where the program raises the error
if array[i] == value:
i = len(array)
return 0
return 1
n = []
len = 25
for i in range(len):
n.append(randint(0,100))
value = n[randint(0,24)]
result = linear_search(n, value) #calling to the function
这里是工作代码:
来自随机导入 *
def linear_search(array, value):
for i in range(len(array)): # line where the program raises the error
if array[i] == value:
i = len(array)
return 0
return 1
n = []
length = 25 #renamed this from len to length
for i in range(length): #renamed this from len to length
n.append(randint(0, 100))
value = n[randint(0, 24)]
result = linear_search(n, value)
您提供的代码在定义此 for 循环时存在问题:
for i in range(len):
n.append(randint(0, 100))
问题:var len
是 build-in 函数 len()
而不是整数
我在 Python 中编写线性搜索代码,但似乎遇到了语法问题。
我有一个接收数组的函数,在函数内部我通过 len 函数获得了该数组的长度。问题是,尽管将数组传递给函数,但它会引发错误 'int' object is not callable.
如果有人能告诉我我做错了什么以及为什么会这样,我将不胜感激。
我在下面留下了我的代码。
from random import *
def linear_search(array, value):
for i in range(len(array)): #line where the program raises the error
if array[i] == value:
i = len(array)
return 0
return 1
n = []
len = 25
for i in range(len):
n.append(randint(0,100))
value = n[randint(0,24)]
result = linear_search(n, value) #calling to the function
这里是工作代码:
来自随机导入 *
def linear_search(array, value):
for i in range(len(array)): # line where the program raises the error
if array[i] == value:
i = len(array)
return 0
return 1
n = []
length = 25 #renamed this from len to length
for i in range(length): #renamed this from len to length
n.append(randint(0, 100))
value = n[randint(0, 24)]
result = linear_search(n, value)
您提供的代码在定义此 for 循环时存在问题:
for i in range(len):
n.append(randint(0, 100))
问题:var len
是 build-in 函数 len()
而不是整数