I am trying to use a linear/sequential search through a list which contains data about users. I need to search for the users by first and last name and then retrieve the remaining list data to be output to the terminal. Linear search is required for this instance as its for an assessment.
So far I have tried the below code:
# Create Child Class
class child:
def __init__(self, childfirstname, childlastname, childdob, childgender, childyearlvl, childethnicity):
self.childfirstname = childfirstname
self.childlastname = childlastname
self.childdob = childdob
self.childgender = childgender
self.childyearlvl = childyearlvl
self.childethnicity = childethnicity
# Create list to allow for Sequential Search
childlist = []
childlist.append(child('Bob','Jones','101015','Male','8','European'))
childlist.append(child('Sally','Fail','200115','Female','8','Maori'))
# Testing output of values stored in childlist
for obj in childlist:
print(obj.childfirstname, obj.childlastname, obj.childdob, obj.childgender, obj.childyearlvl, obj.childethnicity, sep=' ')
print("")
#Sequential search
def search(arr, T):
n = len(arr)#
for i in range (0,n):
if(arr[i] == T):
return i
return -1
result = search(childlist,'Bob')
output = result
if result > -1:
print("Child found: ",childlist[output])
else:
print("Child not found")
I can print the values which have been appended to the list, however, I am not sure how to search for the users obj.childfirstname and obj.childlastname in one statement. Currently it cant find the 'Bob'in the list either so I am certainly doing something wrong. I also tested and found that when I print the Output, it outputs the object and not a string containing all user data. I am new to Python and would appreciate any assistance.