Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I have written out a piece of code but can't seem to get it to work and I'm not sure how to input my data from the dob and turn it into the age. I was wondering how I could write that using my %d as a placeholder for my dob data?

This is the code I have so far but it's saying I am missing an argument.

What I have tried:

from datetime import date

class Person:
  def __init__(self, id, name, dob):
    self.id = id
    self.name = name
    self.dob = dob

  def Greeting(self):
    print("Hello my name is %s" % self.name)  
    #print ("Hello, my name is %s and I am %d years old", %(self.name, self.calcAge()))
    
  def calcAge(dob):
    today = date.today()
    dob = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))

    return dob
  print("I am %d" % calcAge(), 'years old')
   
p1 = Person(2315, "John", "6/13/2005")
p1.calcAge()
p1.Greeting()
print(p1.Greeting)
p1.id = 4506
p1.name = "Bob"
p1.dob = '3/8/2003'
p1.calcAge()
Posted
Updated 1-Nov-22 22:10pm

Look at your code:
Python
  def calcAge(dob):
    today = date.today()
    dob = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))

    return dob
...
p1.calcAge()
You define the method as taking a single value as a parameter and returning the age. (Sort of)
Then you call it without giving it the parameter at all! Understandably, it complains ...
Additionally, when you call it, you don't do anything with the answer you return.
 
Share this answer
 
Comments
CPallini 2-Nov-22 3:28am    
5.
You are using strings for your date fields so this code will never work. You need to convert the strings to Date types, or use Date types in the firs place; see datetime — Basic date and time types — Python 3.11.0 documentation[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900