Python Forum
Calculate AGE in years
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate AGE in years
#11
I used the timedealta. Obviously, one day is missing.
datetime.date(2021, 12, 31) - datetime.date(2021, 1, 1)
Output:
datetime.timedelta(days=364)
The correct result:
datetime.date(2022, 1, 1) - datetime.date(2021, 1, 1)
Output:
datetime.timedelta(days=365)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
(Apr-25-2023, 08:37 AM)DeaD_EyE Wrote: I used the timedealta. Obviously, one day is missing.
Obviously datetime.date(2021, 1, 1) is not being counted, i.e. year "starts" at 2 January up to and incl. 31 December.
>>> import datetime
>>> datetime.date(2021, 12, 31) - datetime.date(2020, 12, 31)
datetime.timedelta(days=365)
>>> datetime.date(2020, 12, 31) - datetime.date(2019, 12, 31) 
datetime.timedelta(days=366)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
Hello now i want make a new subclass Student and every student get a new number (increase but get the following error

from datetime import datetime, date

# Informatie persoon naam sekse en geboortdatum
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
    
# Afvraag man of vrouw
  def isMan(self):
    if self.sekse == "M":
      return True
    else:
      return False   
    
# Leeftijd bepalen    
  def leeftijd(self):
    today = date.today()
    age = (today.year - self.geboortedatum.year) 
    return age 

# Subklasse docent  
class Docent(persoon):
    def __init__(self, naam, sekse, geboortedatum, Salaris=0):
        super().__init__(naam, sekse, geboortedatum,)
        self.Salaris = Salaris

    def setSalaris(self, SalarisInput):
      self.Salaris = SalarisInput
  
    def getSalaris(self):
      return self.Salaris

    # Salaris verhogen 
    def verhoogSalaris(self, percentage):
      newSalaris = (self.Salaris / 100) * percentage + self.Salaris
      self.Salaris = newSalaris
 
# Subklasse Student 
class Student(persoon):
    def __init__(self, naam, sekse, geboortedatum):
        super().__init__(naam, sekse, geboortedatum)
        self.StudentNr = Nr + 1

 
    def getStudentNr(self):
      return self.StudentNr

s1 = Student("Dennis", "m", "24-02-2000")
print(s1.getStudentNr(),s1.getNaam(), s1.leeftijd())
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_FeedbackVraag6.py] Traceback (most recent call last): File "C:\Python\Objectgeoriënteerd programmeren_FeedbackVraag6.py", line 71, in <module> s1 = Student("Dennis", "m", "24-02-2000") File "C:\Python\Objectgeoriënteerd programmeren_FeedbackVraag6.py", line 65, in <module> self.StudentNr = Nr + 1 builtins.NameError: name 'Nr' is not defined
Reply
#14
That is a descriptive error message. Nr is not defined. You cannot add 1 to something that is not defined. You will need to define something that you can increment each time an instance is created. You will need to create a class variable that is the counter. You should try an internet search.
Reply
#15
I try this

class Student(persoon):
  StudentNr = 0
  def __init__(self, naam, sekse, geboortedatum):
    super().__init__(naam, sekse, geboortedatum)
      Student.StudentNr += 1
      self.StudentNr = Student.StudentNr
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_FeedbackVraag6.py] Traceback (most recent call last): File "C:\Python\Objectgeoriënteerd programmeren_FeedbackVraag6.py", line 65, in <module> Student.StudentNr += 1 builtins.IndentationError: unexpected indent (x-wingide-python-shell://86941368/2, line 65)
Reply
#16
I found it it was a space
Reply
#17
Are Students allowed a Salaris? If not, they should be a subclass pf persoon, not Docent. If they are allowed a Salaris, should there be a Salaris argument to the __init__() method.
Reply
#18
I fail to see why leap year matters at all.
Quote:# correct the bday, if it's the 29th february and today is not a leap yaar
if bday.month == 2 and bday.day == 29 and not calendar.isleap(today.year):
bday = datetime.date(bday.year, 3, 1)
If you were born on leap day, and today is leap day, (today.month, today.day) >= (bday.month, bday.day) is True.
If this is not a leap year and today is March 1, (today.month, today.day) >= (bday.month, bday.day) is True.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 3,661 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  Problem with code to calculate weekday for leap years! Event 2 2,798 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 9,848 Mar-26-2018, 04:44 AM
Last Post: PyMan
  human years to dog years jhall710 7 11,357 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