Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Computer Science Introduction to Algorithms Algorithms in Code Linear Search in Code

Michelle Rosenstock
PLUS
Michelle Rosenstock
Courses Plus Student 3,256 Points

Keeping getting "target not found" Linear Search Practice

Hi,

I keep get the response "target not found" when I updated the verify function to include 6 as an argument. Please advise why this coding isn't working. Thank you.

def linear_search(list, target): """ Returns the index position of the target if found, else returns None """

for i in range(0,len(list)): if list[i]==target: return i return None

def verify(index): if index is not None: print("Target found at index: ", index) else: print("Target not found in list")

numbers=[1,2,3,4,5,6,7,8,9,10]

result=linear_search(numbers,12) verify(result) result=linear_search(numbers,6) verify(result)

1 Answer

Check your indenting, particularly the return None statement, and see if it matches the following:

def linear_search(list, target):
    for i in range(0,len(list)):
        if list[i]==target:
            return i
    return None

def verify(index):
    if index is not None:
        print("Target found at index: ", index)
    else:
        print("Target not found in list")

numbers=[1,2,3,4,5,6,7,8,9,10]

result=linear_search(numbers,12)
verify(result)
result=linear_search(numbers,6)
verify(result)