Access to class objects

Asked

Viewed 80 times

1

class human(object):
    kind = 'humam'
    def __init__(self,name, sex, age):
        self.name = name
        self.sex = sex
        self.age = age
    def report(self):
        print(self.name)
        print(self.sex)
        print(self.age)
com = int(input('What would you like to do: \n 1) Add a contact \n 2) Info on a contact \n 3) Break\n'))
while com != 3:
    if com == 1:
        name,sex,age = input('Enter name sex age: ').split()
        name = human(name,sex,age)
    if com == 2:
        name = input('Enter name: ')
        name.report()
    com = int(input('What would you like to do: \n 1) Add a contact \n 2) Info on a contact \n 3) Break\n'))

After defining some Uman objects, I could not access them if not during the same execution, for example, selecting 1 to add contact, once instantiated john = Uman('john','Male','20') When selected 2 to check the information of the object appears the following error: name.report() Attributeerror: 'str' Object has no attribute 'report' How can I fix this?

  • 2

    In fact, the instance does not get the name john, but yes name. What you are doing is storing only one object human in a variable name when the option is 1, but when it is 2, you store the name of the person in name, making it lose the reference to the object, becoming a string.

2 answers

0

You’re trying to use the method report in something that is not an object of the Human class. Actually within its if com == 2, you write that name is equal to an input. That is, you are calling the method report for a string, and strings have no method report. Explain better what you want to do with this code that might help you better.

0

When you build a human stores it in a variable called name

if com == 1:
    ...
    name = human(name,sex,age) #aqui o novo human é guardado em name

The problem starts right away in the variable name, which should be intuitive to its content. If it keeps a human should not be called name because it is misleading, and represents part of the problem that has.

Further down this name receives a string:

if com == 2:
    name = input('Enter name: ') #aqui guarda a string por cima do human que tinha

Thus name has a string and not a string human, just as the next one tries to call the method report:

 name.report()

This failure because it no longer refers to a human.

Correcting and improving

From what I can see from your program, you’re trying to get information from people already registered. To do this it is necessary to keep each person on a list:

humans = [] #lista para todos os humans registados

Then when you add a new person, add it to the list using the function append. To show a person you have to go through the list and if the name entered matches that of some list element, show that element with the method report:

while com != 3:
    if com == 1:
        name,sex,age = input('Enter name sex age: ').split()
        humans.append(human(name,sex,age)) #adiciona o novo human a lista
    if com == 2:
        name = input('Enter name: ')
        for somehuman in humans: #percorrer todos os humans
            if name in somehuman.name: #se o nome recebido está no nome do human
                somehuman.report() #chama o report nesse human
    com = int(input('What would you like to do: \n 1) Add a contact \n 2) Info on a contact \n 3) Break\n'))

Example in Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.