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


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,428 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  Using classes? Can I just use classes to structure code? muteboy 5 5,190 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Question about classes cybercreature 2 3,542 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