Python Forum
Calculate AGE in years
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate AGE in years
#5
Other question, I try to make a sub classes and when there is no argument then value 0
But get the following error

from datetime import datetime, date

class persoon:
  def __init__(self, naam, sekse, geboortedatum):
    self.naam = naam
    self.sekse = sekse
    self.geboortedatum = datetime.strptime(geboortedatum, '%d-%m-%Y').date()
  
  def getNaam(self):
    return self.naam

  def getGebDatum(self):
    return self.geboortedatum 
  
  def isVrouw(self):
    if self.sekse == "V":
      return True
    else:
      return False

  def isMan(self):
    if self.sekse == "M":
      return True
    else:
      return False   
    
  def leeftijd(self):
    today = date.today()
    age = (today.year - self.geboortedatum.year) 
    return age 

class Docent(persoon):
    def __init__(self, naam, sekse, geboortedatum,*Salaris):
      if(Salaris==None):
          self.Salaris = "0"
      else:
          self.Salaris = Salaris

d1 = Docent("John", "M", "24-04-2000")

print(d1.naam)
Error:
3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] Python Type "help", "copyright", "credits" or "license" for more information. [evaluate Objectgeoriënteerd programmeren_FeedackVraag4.py] Traceback (most recent call last): File "C:\Python\Objectgeoriënteerd programmeren_FeedackVraag4.py", line 43, in <module> print(d1.naam) builtins.AttributeError: 'Docent' object has no attribute 'naam'
deanhystad write Apr-24-2023, 06:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Calculate AGE in years - by Kessie1971 - Apr-24-2023, 07:31 AM
RE: Calculate AGE in years - by DeaD_EyE - Apr-24-2023, 09:46 AM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 05:07 PM
RE: Calculate AGE in years - by DeaD_EyE - Apr-24-2023, 05:28 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 06:05 PM
RE: Calculate AGE in years - by deanhystad - Apr-24-2023, 06:22 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 06:36 PM
RE: Calculate AGE in years - by deanhystad - Apr-24-2023, 07:20 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-24-2023, 07:23 PM
RE: Calculate AGE in years - by buran - Apr-25-2023, 06:35 AM
RE: Calculate AGE in years - by DeaD_EyE - Apr-25-2023, 08:37 AM
RE: Calculate AGE in years - by buran - Apr-25-2023, 08:55 AM
RE: Calculate AGE in years - by Kessie1971 - Apr-25-2023, 05:00 PM
RE: Calculate AGE in years - by deanhystad - Apr-25-2023, 05:33 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-25-2023, 05:44 PM
RE: Calculate AGE in years - by Kessie1971 - Apr-25-2023, 05:56 PM
RE: Calculate AGE in years - by deanhystad - Apr-25-2023, 07:15 PM
RE: Calculate AGE in years - by deanhystad - Apr-26-2023, 03:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 4,081 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  Problem with code to calculate weekday for leap years! Event 2 2,973 Dec-15-2018, 05:13 PM
Last Post: Event
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 10,172 Mar-26-2018, 04:44 AM
Last Post: PyMan
  human years to dog years jhall710 7 11,658 May-08-2017, 05:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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