I was coding a linear search in Python, but it seems I got a syntax problem.
I have a function which receives an array, and inside the function I got the lenght of that array throught len function. The problem is that, despite being passing an array to the function it raises an error 'int' object is not callable.
I would highly appreciate it someone could tell me what did I did wrong and why this is happening.
I left my code below.
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
Here the working code:
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 = []
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)
the code you have given had the issue that when defining this for loop:
for i in range(len):
n.append(randint(0, 100))
Issue: The var len
is the build-in function len()
and not an integer