Q:

can someone explain this function to me

I wrote this function but...
def look_up(to_search,target):
    for (index , item) in enumerate(to_search):
        if item == target:
             break
        
    else:
        return(-1)
    return(index)
    

when I pass in a name that's on that list it returns the index of that name but when i pass in a wrong name it returns -1 and doesn't return the "return(index )"even tho the "return(index)" statement is out of the for loop why is that?? and also I can't add anything else to the "ELSE" statement, I tried to add a "print" statement to the else but it doesn't print it.
names=['neno', 'jay', 'james,'alex','adam','robert','geen']

name_index=look_up(names,"geen")

print(name_index)

print("the name is at location: {}".format(name_index))

A:

def look_up(to_search,target):
    for (index , item) in enumerate(to_search):
        if item == target:
             break # break statement here will break flow of for loop 
        else:
            return(-1)  # when you write return statement in function function will not execute any line after it

    return(index)
    print("This line never be printed")  # this line is below return statement which will never execute

However you can find index of function with names.index("NAME") for more you can implement function as follow:
def look_up_new(search_in, search_this):
    if search_this in search_in:
        return search_in.index(search_this) # returns index of the string if exist
    else:
        print("NOT EXIST") # you can print this line in else block also where I have written False
        return False

names=['neno', 'jay', 'james','alex','adam','robert','geen']

name_index = look_up_new(names, "alex")

if name_index:
    print("the name is at location: {}".format(name_index))
else:
    pass # if you are not willing to doing anything at here you can avoid this loop

