Python Forum
Class Method to Calculate Age Doesn't Work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Method to Calculate Age Doesn't Work
#1
Hello Everyone,

I'm new to Python programming, I need your help to fixing a code I'm working. It's a class that has a method that calculate a person age (instance of the class). This is my code:
from datetime import date

class personAge:
    def __init__(self, name, birthdate):
        self.name = name
        self.birthdate = birthdate
    def Name(self):
        return self.name

    def calc_age(self):
        today = date.today()
        over_trashold = (today.month, today.day) < (self.birthdate.month, self.birthdate.day)
        if over_trashold:
            return today.year - self.birthdate.year - 1
        return today.year - self.birthdate.year    
def main():
    person1 = personAge()
    person1.name = "Georges"
    person1.birthdate = date(1994, 12, 25)
    print(person1.name, person1.birthdate)


if __name__ == "__main__":
    main()
This is the error message I'm getting:
Error:
Traceback (most recent call last): File "class_age.py", line 24, in <module> main() File "class_age.py", line 17, in main person1 = personAge() TypeError: __init__() missing 2 required positional arguments: 'name' and 'birthdate'
Thank you
Reply
#2
Your __init__ requires the arguments name and birthdate being passed to it.
The convention is for Class's to be CapWords and methods to be lower case.
The method Name is not required.
class PersonAge:
    def __init__(self, name, birthdate):
        self.name = name
        self.birthdate = birthdate

    def calc_age(self):
        today = date.today()
        over_trashold = (today.month, today.day) < (
            self.birthdate.month,
            self.birthdate.day,
        )
        if over_trashold:
            return today.year - self.birthdate.year - 1
        return today.year - self.birthdate.year


def main():
    name = "Georges"
    birthdate = date(1994, 12, 25)
    person1 = PersonAge(name, birthdate)
    print(person1.name, person1.birthdate)
    print(person1.calc_age())


if __name__ == "__main__":
    main()
Output:
Georges 1994-12-25 26
gdbengo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PIP doesn't work YKR 1 553 Mar-28-2025, 02:10 PM
Last Post: snippsat
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 1,582 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Extending list doesn't work as expected mmhmjanssen 2 1,320 May-09-2024, 05:39 PM
Last Post: Pedroski55
  class definition and problem with a method HerrAyas 2 1,388 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 2,299 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,876 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 3,418 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Using one child class method in another child class garynewport 5 3,126 Jan-11-2023, 06:07 PM
Last Post: garynewport
  color code doesn't work harryvl 1 1,777 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 2,549 Jun-16-2022, 07:59 AM
Last Post: gerald

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020