Reputation: 1
array1 = []
size = int(input("ENTER SIZE"))
print("ENTER VALUES")
for i in range(0,size):
l = int(input())
array1.append(l)
x = int(input("ENTER SEARCH ELEMENT"))
def linearsearch(array1, size, x):
pos = -1
i = 0
while i < size:
if(array1.index(i) == x):
pos = i
break
else:
continue
if(pos!=-1):
print(pos)
else:
print(pos)
I have used while loop to find the element position corresponding with the search element x But the pycharm console doesnt print anything after the input please help
Upvotes: 0
Views: 79
Reputation: 3091
It is obvious from the code that the last output prompt will be
ENTER SEARCH ELEMENT
This is because you only define linearsearch
but never call it. Try adding this line at the end:
linearsearch(array1, size, x)
The question has a number of issues IMO:
Actually there is output generated (the prompts for input), so the question asserts something that is not entirely correct.
BTW: Even if you add the call to linearsearch, the code still has some bugs:
array1.index(i)
does not return the item at index i
but does a linear search for an item with value i
. You meant array1[i]
.i = i + 1
instead of continue
.Upvotes: 0