Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I actaully want to make a linear search program in python but i am actually stuck, and the code I develop isn't working well

What I have tried:

Python
def linear_Search(name,target):
    for i in range(len(name)):
        if name[i]==target:
            return i
        else:
            return -1

L=eval(input("Enter the list : "))
target=eval(input("Enter the item need to be searched : "))
r=linear_Search(L,target)
print(r)
Posted
Updated 27-Nov-23 15:21pm
v2

Google search is a good place to start. This search has many examples for you: python linear search program - Google[^]
 
Share this answer
 
You have to examine the whole list, before giving up the search.
Change from
Quote:
def linear_Search(name,target):
for i in range(len(name)):
if name[i]==target:
return i
else:
return -1
to
Python
def linear_Search(name,target):
    for i in range(len(name)):
        if name[i]==target:
            return i
    return -1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900