you are almost there, apart from poor choice of weight1 as property name
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
# 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-objectsto 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
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs