Thymas
Thymas

Reputation: 49

Search with input in Object Python

I have an assigment where i need to search through an object.

class Person:
    navn=None
    tlf=None

    def __init__(self, navn, tlf):
        self.navn = navn
        self.tlf = tlf

def printname(self):
    print(self.navn, self.tlf)

Kari = Person("Kari", 98654321)
Liv = Person("Liv", 99776655)
Ola = Person("Ola", 99112233)
Anne = Person("Anne", 98554455)
Jens= Person("Jens", 99776612)
Per = Person("Per", 97888776)
Else = Person("Else", 99455443)
Jon = Person("Jon", 98122134)
Dag = Person("Dag", 99655732)
Siv = Person("Siv", 98787896)

So my question is: How can i search with a user input to find a person because i have noticed that you can not search the same way as in lists. so i cant do:

def search():
test = input("Test ")
printname(test)

Upvotes: 0

Views: 404

Answers (4)

martineau
martineau

Reputation: 123511

You need to put all the Person instances in some sort of a container, like a list or dictionary. Since you want to lookup the instance by what their navn attribute is, a dictionary makes more sense since you could make its keys the same as the attribute which would reduce the "search" to a simple lookup. Using a list would require iterating through the Person instances in the list until one with a matching attribute was found — a much slower process.

Here's a solution show how this could be done:

class Person:
    def __init__(self, navn, tlf):
        self.navn = navn
        self.tlf = tlf

    def printname(self):
        print(self.navn, self.tlf)


def search(persons):
    navi = input("navi? ")
    try:
        persons[navi].printname()
    except KeyError:
        print(navi + ' not found')

people = dict(
    Kari = Person("Kari", 98654321),
    Liv = Person("Liv", 99776655),
    Ola = Person("Ola", 99112233),
    Anne = Person("Anne", 98554455),
    Jens = Person("Jens", 99776612),
    Per = Person("Per", 97888776),
    Else = Person("Else", 99455443),
    Jon = Person("Jon", 98122134),
    Dag = Person("Dag", 99655732),
    Siv = Person("Siv", 98787896),
)

search(people)

Upvotes: 1

MengMeng
MengMeng

Reputation: 1024

class Person:
navn=None
tlf=None

def __init__(self, navn, tlf):
    self.navn = navn
    self.tlf = tlf

def printname(self):
    print(self.navn, self.tlf)

Kari = Person("Kari", 98654321)
Liv = Person("Liv", 99776655)
Ola = Person("Ola", 99112233)
Anne = Person("Anne", 98554455)
Jens= Person("Jens", 99776612)
Per = Person("Per", 97888776)
Else = Person("Else", 99455443)
Jon = Person("Jon", 98122134)
Dag = Person("Dag", 99655732)
Siv = Person("Siv", 98787896)

all = [Kari,Liv,Ola,Anne,Jens,Per,Else,Jon,Dag,Siv]

def search(name):
    return [x for x in all if x.navn==name]


ret = search('Jon')

for i in ret:
    i.printname()

Upvotes: 2

DirtyBit
DirtyBit

Reputation: 16792

Make a dict, take the input, split by . search for the name in the dict:

class Person:
    def __init__(self, navn, tlf):
        self.navn = navn
        self.tlf = tlf

def printname(self):
    print(self.navn, self.tlf)

persons = {
    'Kari':  Person("Kari", 98654321),
    'Liv':   Person("Liv", 99776655),
    'Ola':   Person("Ola", 99112233),
    'Anne':  Person("Anne", 98554455),
    'Jens':  Person("Jens", 99776612),
    'Per':   Person("Per", 97888776),
    'Else':  Person("Else", 99455443),
    'Jon':   Person("Jon", 98122134),
    'Dag':   Person("Dag", 99655732),
    'Siv':   Person("Siv", 98787896)
}

def search():
    name = input('Enter a name: ')
    for key, val in persons.items():
        if key == name.split('.')[0]:
            print(key)

search()

OUTPUT:

Enter a name: Kari.navn
Kari

Upvotes: 3

Nullman
Nullman

Reputation: 4279

I think I understand what you are trying to do. in this case, a dictionary will be useful.

people = {}
people['Kari'] = Person("Kari", 98654321)
people['Liv'] = Person("Liv", 99776655)
people['Ola'] = Person("Ola", 99112233)
people['Anne'] = Person("Anne", 98554455)
people['Jens'] = Person("Jens", 99776612)
people['Per'] = Person("Per", 97888776)
people['Else'] = Person("Else", 99455443)
people['Jon'] = Person("Jon", 98122134)
people['Dag'] = Person("Dag", 99655732)
people['Siv'] = Person("Siv", 98787896)

def search():
    test = input("Test ")
    dude = people[test]
    print(dude.navn, dude.tlf)

make sure to add exception handling to your search funciton, becuase it can throw keyerrors

Upvotes: 3

Related Questions