Python Forum
Classes and Objects - question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes and Objects - question
#2
you are almost there, apart from poor choice of weight1 as property name

# Create an object to compute John's weight in Kgs as well as age in years
import datetime
class User:
    def __init__(self, full_name, weight, birthday):
        self.name = full_name
        self.weight = weight
        self.birthday = birthday
     
    def age(self):
        today = datetime.date.today()
        yyyy = int(self.birthday[0:4])
        mm = int(self.birthday[4:6])
        dd = int(self.birthday[6:8])
        dob = datetime.date(yyyy, mm, dd)
        age_in_days = (today-dob).days
        age_in_years = age_in_days/365
         
        return int(age_in_years)
         
    def weight1(self):
        weight_in_kgs = self.weight * 0.454
        return int(weight_in_kgs)
        
john = User('John Doe', 175, '19801101')
print(john.weight1())
print(john.age())
Output:
79 38
As to your question 1 - read this https://docs.python.org/3.7/tutorial/cla...ce-objects
to understand the difference between attribute and method.

You can (and it's better to do so) and use some more advanced technique if you want age and weight to be properties, e.g.
# Create an object to compute John's weight in Kgs as well as age in years

import datetime
class User:
    def __init__(self, full_name, weight, birthday):
        self.name = full_name
        self.weight = weight
        self.birthday = birthday
    
    @property
    def age(self):
        today = datetime.date.today()
        yyyy = int(self.birthday[0:4])
        mm = int(self.birthday[4:6])
        dd = int(self.birthday[6:8])
        dob = datetime.date(yyyy, mm, dd)
        age_in_days = (today-dob).days
        age_in_years = age_in_days/365
        return int(age_in_years)
         
    @property
    def weight_kg(self):
        weight_in_kgs = self.weight * 0.454
        return int(weight_in_kgs)
        
john = User('John Doe', 175, '19801101')
print(john.weight_kg)
print(john.age)
in addition you may simplify the calculation of age significantly by using datetime module objects and methods, e.g.

import datetime
class User:
    def __init__(self, full_name, weight, birthday):
        self.name = full_name
        self.weight_lbs = weight
        self.birthday = datetime.date.fromisoformat(birthday)
    
    @property
    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthday.year
        if self.birthday.replace(year=today.year) <= today:
            return  age
        else:
            return age - 1 

    @property
    def weight_kgs(self):
        weight_kg = self.weight_lbs * 0.454
        return round(weight_kg, 1)
        
john = User('John Doe', 175, '1980-12-01')
print(john.weight_kgs)
print(john.age)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Classes and Objects - question - by AI_ML_Neophyte - Dec-06-2018, 02:29 PM
RE: Classes and Objects - question - by buran - Dec-06-2018, 03:31 PM
RE: Classes and Objects - question - by wavic - Dec-11-2018, 01:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Listing Attributes of Objects & Classes JoeDainton123 4 2,389 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  Using classes? Can I just use classes to structure code? muteboy 5 5,142 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Question about classes cybercreature 2 3,522 May-18-2017, 07:27 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020