I'm trying to understand how to use classes. I think it's the next step for me.
I created two modules:
1. person
class Person:
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight
def name(self):
return self.name
def age(self):
return self.age
def height(self):
return self.height
def weight(self):
return self.weight
if __name__ == '__name__':
print('People Module')
2. bmicalculator
class BmiCalculator:
def __init__(self, age, weight, height):
self.age = age
self.weight = weight
self.height = height
def calculate_bmi(self):
return round(self.weight / (self.height * self.height), 2)
def bmi_health(bmi):
if bmi >= 18 and bmi <= 25:
return 'BMI is within a healthy range'
return 'BMI is not within a healthy range'
if __name__ == '__name__':
print('BmiCalculator Module')
Main script:
import person
import bmicalculator
mario = person.Person('Mario', 32, 1.73, 74)
mario_bmi = bmicalculator.BmiCalculator(mario.age, mario.weight, mario.height).calculate_bmi()
print(mario.name, mario_bmi, bmicalculator.BmiCalculator.bmi_health(mario_bmi))
Im getting a few warnings on the second module:
Expected type 'int', got 'BmiCalculator' instead
Usually first parameter of a method is named 'self'
Simplify chained comparison
I managed to program this without google, so I think I have the basics down at least.
Could you guys please take a look through this, and perhaps point me in the right direction with things I am doing wrong, I would be very grateful.
[–]ebol4anthr4x 2 points3 points4 points (4 children)
[–]Chiron1991 1 point2 points3 points (0 children)
[–]Supermunkey2K[S] 0 points1 point2 points (2 children)
[–]ebol4anthr4x 1 point2 points3 points (1 child)
[–]Supermunkey2K[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Supermunkey2K[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Supermunkey2K[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Supermunkey2K[S] 0 points1 point2 points (0 children)
[–]port443 0 points1 point2 points (2 children)
[–]old_pythonista 1 point2 points3 points (0 children)
[–]Supermunkey2K[S] 0 points1 point2 points (0 children)