Accessing objects of a class

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 objects of human, I could not access them if it was not during the same execution, for example, selecting 1 to add contact, once instantiated john = human ('john', 'male', '20' ) When you select 2 to check the object information the following error appears:     name.report () AttributeError: 'str' object has no attribute 'report' How can I resolve this?

    
asked by anonymous 28.10.2017 / 03:57

2 answers

0

You are trying to use the report method on something that is not an object of the human class. Actually within your if com == 2 , you write that name equals an input. That is, you are calling the report method for a string, and strings have no report method. It explains better what you want to do with this code that may help you better.

    
28.10.2017 / 04:29
0

When constructing a human , store it in a variable called name

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

The problem begins immediately in the name of the variable, which should be intuitive for its contents. If it holds a human it should not be called name because that induces in error, and represents part of the problem that it has.

Below this name gets a string:

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

So name has a string and not a human , so when you try to call the report method:

 name.report()

This fails because it no longer refers to human .

Correcting and improving

From what I can see from your program you are trying to get information from people already registered. To do this, you must save each person in a list:

humans = [] #lista para todos os humans registados

Then when you add a new person, you add it to the list using the append function. To show a person has to scroll through the list and if the name entered matches that of some element list, show this 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 on Ideone

    
28.10.2017 / 13:34