-1

I am trying to write a class that determines if a person is an adult or not. If a person is an adult then they are 18 or older. And the program should return True. However, I keep getting a return value of False when I implement this program. If anyone has any ideas of what I am doing wrong it would be much appreciated.

class Person:

    def __init__(self, name, adult=False, age=0):
        self.name = name
        self.age = age
        self.adult = adult

        if self.age >= 18:
            self.adult = True
        else:
            self.adult = False

    def birthday(self):
        self.age += 1

p = Person("Dan", 17)
p.adult
p.birthday()
p.adult

By my understanding the birthday function should increment the age by 1 when it is called but I do not think it is doing that and I am unsure as to why.

DataD96
  • 301
  • 3
  • 12
  • *And the program should return True* - post the context of the program usage – RomanPerekhrest Nov 23 '19 at 19:17
  • What? The program is not being used for anything. Strictly for learning purposes only? Is that what you mean? – DataD96 Nov 23 '19 at 19:22
  • When you said "program should return True" - you need to realize what would invoke that program action. If you did not event try to instantiate your class - your considerations are premature – RomanPerekhrest Nov 23 '19 at 19:25
  • Oh I see what you are saying now. yes, I did instantiate my class. I just did not put it in the code above, I have edited the code above now to include the entire program. Even so, the program is still returning false. When I call p.adult it should check to see if the person is an adult and return the correct output. Then when I call birthday it should increment the age by one and when I call p.adult again it should return True when I use the instantiated code above. – DataD96 Nov 23 '19 at 19:32
  • What it should do is store the date-of-birth, and `.age` and `.adult` should be functions. That way, the age and adult status will automatically be correct. – Andrew Morton Nov 23 '19 at 19:38

1 Answers1

0

Corrections in steps:

  • __init__ special method is called only at once on a class instantiation phase.
    That's why self.adult in your implementation will be assigned just once
  • the conditon:

    if self.age >= 18:
        self.adult = True
    else:
        self.adult = False
    

    is just a noisy/verbose version of self.adult = self.age >= 18

  • passing adult=False into constructor makes no sense as adult property is conceptually dependent on age value

  • adult attribute is better defined as calculated property and accessed through @property decorator - that'll make it behave like dynamic attribute

Optimized design:

class Person:

    def __init__(self, name, age=0):
        self.name = name
        self.age = age
        self._adult = False

    @property
    def adult(self):
        return self.age >= 18

    def birthday(self):
        self.age += 1


p = Person("Dan", 17)
print(p.adult)   # False
p.birthday()
print(p.adult)   # True
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105