0

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.

  • Have you tried printing `arr[i]` and `T` before comparing them? Do you see why they don't match? – Axe319 Dec 15 '22 at 03:46
  • " however, I am not sure how to search for the users obj.childfirstname and obj.childlastname in one statement." To be clear: the question is, given an `obj`, how to write code that determines whether `obj.childfirstname` is equal to `T` or `obj.childlastname` is equal to `T`? I assume you know how to do each of those comparisons separately. Did you try using `or` to combine them? – Karl Knechtel Dec 15 '22 at 03:54
  • Or perhaps you needed to check separate values for the first name and last name. In that case - think carefully about how the `search` function is defined. What is the purpose of `T`? Does it represent a first name, or a last name? (Do you see a problem here?) What inputs does the function need? Therefore, what should its parameters look like? – Karl Knechtel Dec 15 '22 at 03:56

0 Answers0