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
solved can someone explain this function to me