Calculate AGE in years - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Calculate AGE in years (/thread-39854.html) Pages:
1
2
|
RE: Calculate AGE in years - DeaD_EyE - Apr-25-2023 I used the timedealta . Obviously, one day is missing.datetime.date(2021, 12, 31) - datetime.date(2021, 1, 1) The correct result:datetime.date(2022, 1, 1) - datetime.date(2021, 1, 1)
RE: Calculate AGE in years - buran - Apr-25-2023 (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) RE: Calculate AGE in years - Kessie1971 - Apr-25-2023 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())
RE: Calculate AGE in years - deanhystad - Apr-25-2023 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. RE: Calculate AGE in years - Kessie1971 - Apr-25-2023 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
RE: Calculate AGE in years - Kessie1971 - Apr-25-2023 I found it it was a space RE: Calculate AGE in years - deanhystad - Apr-25-2023 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. RE: Calculate AGE in years - deanhystad - Apr-26-2023 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 yaarIf 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. |