1
class Artist:
    def __init__(self, name, dob):
        self.name = name
        self.dob = dob

    def get_name(self):
        return self.name

    def get_dob(self):
        return self.dob

    def age(self):
        if get_date_today() < (2013, 12, 27):
            return self.age
        else:
            return self.age + 1


def get_date_today():
    return (2013, 10, 30) #'today'

hw = Artist("Hayley Williams", (1988, 12, 27))
print(hw.age())       # 24 if 'today' is < (2013, 12, 27), 25 if 'today' is >= (2013, 12, 27) 

How do I do an addition in the else loop. It can't work here because I can't add 1 to the method. So what is wrong ?

user3398505
  • 607
  • 1
  • 7
  • 13
  • There is no such thing as `self.age` in your `__init__`. Why? – sshashank124 Apr 02 '14 at 08:31
  • Please convert your tuple to data and perform date operation. You will get difference in days and convert it in years. – Nilesh Apr 02 '14 at 08:32
  • Your code has other semantic errors in it (like a hardcoded `today`), that's why you will hardly get a correct answer. Please try to explain what are you trying to achieve first. – Selcuk Apr 02 '14 at 08:35
  • see all the confusion below, ... dob is probably date of birth ... picking names for variables is SO important... – Paul Apr 02 '14 at 08:56

3 Answers3

1

You do not have a self.age variable in your __init__ loop. Try this instead:

class Artist:
    def __init__(self, name, dob, age):
        self.name = name
        self.dob = dob
        self.age = age

    def get_name(self):
        return self.name

    def get_dob(self):
        return self.dob

    def get_age(self):
        if get_date_today() < (2013, 12, 27):
            return self.age
        else:
            return self.age + 1
sshashank124
  • 31,495
  • 9
  • 67
  • 76
1
from datetime import datetime
class Artist:
    def __init__(self, name, dob):
        self.name = name
        self.dob = dob

    def get_name(self):
        return self.name

    def get_dob(self):
        return self.dob

    def age(self):
        return ((datetime.today() - datetime(*self.dob)).days)/365

datetime is module in python which use to perform date realted operation. datetime.today will return you current date.

Nilesh
  • 20,521
  • 16
  • 92
  • 148
0

You are trying to return an age in your method age() which you have no data about. Decide where to get that data from and store it, then you can return it in the method.

One way to achieve this is to get the data upon object construction:

def __init__(self, birthday):
  self.birthday = birthday
  # store also your other data like name and dob

then you can use it to compute the age:

def age(self):
  if get_date_today() < (2013, 12, 27):
    return get_date_today() - self.birthday
  else:
    return get_date_today() - self.birthday + 1
  # this assumes of course that you are using a date type which understands subtraction

But there are other ways like getting the age from a data base or just returning the age given on construction (see sshashank124's answer for that).

Alfe
  • 56,346
  • 20
  • 107
  • 159