Python Forum
Classes and Objects - question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes and Objects - question
#1
Hi,

I'm teaching myself Python and am having trouble with Classes and Objects.

Please see below my Python Code.

I'm struggling to figure out how to create a weight1 field that gives weight in Kgs.

Two specific questions:
1) How do I create the weight1 field?
2) Why does the invocation of age have "age()"?

thanks in advance

PC

# Create an object to compute John's weight in Kgs as well as age in years
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(2018, 12, 5)
        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 = weight/0.454
        return int(weight_in_kgs)    
Reply
#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
#3
thank you. Much appreciated.

PC
Reply
#4
I was thinking that this is a Python forum. Am I wrong? Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Listing Attributes of Objects & Classes JoeDainton123 4 2,317 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Question about classes cybercreature 2 3,426 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