Here is a piece of code that have been rewritten:
def nameSearch(list_names, name):
len_student = len(list_names)
for i in range (0,9):
if (list_names[i] == name):
print(i)
break
elif (list_names[i] != name):
return -1
names = ""
names_list = ["Bob", "John", "Jack", "Alice", "Jane"]
length_name = len(names_list)
names = input("Which student do you want to find: ")
nameSearch(names_list, names)
So while it is going through the loop, the computer will do one of the argument:
First one, if the name is in the list it will print the position number that is in the array. Although if the name the user inputted is not on the list, than it will print -1.
The problem with this code is only checks the first one, as I attempted to use break and return function, as this is giving me not the desired answer.
I want to make sure that it loops through until it finds the user, and than breaks. Instead of looping and breaking in the first element of the array.

New Topic/Question
Reply


MultiQuote





|