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
  class definition and problem with a method HerrAyas 2 253 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 737 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 940 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,792 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Using one child class method in another child class garynewport 5 1,583 Jan-11-2023, 06:07 PM
Last Post: garynewport
  color code doesn't work harryvl 1 891 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,715 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,206 May-30-2022, 03:31 PM
Last Post: bowlofred
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,314 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,467 Feb-27-2022, 09:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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