Python Forum

Full Version: Calculate AGE in years
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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)
(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)
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
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.
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)
I found it it was a space
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.
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.
Pages: 1 2