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): = id = name
    self.dob = dob

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

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

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

    return dob
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
CPallini 2-Nov-22 3:28am    
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