Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can Someone Please Expian How You Pass Objects In Python

class Input:
    def promptMessage(self,name,measure):
        
        self.name = name
    
        
        while True:
            try:
                num = int(input("Please Enter Your {} in {} ".format(name,measure)))
            except ValueError:
                print("Please enter a number")
            else:
                break
        return num
            
class Person:
    def __init__(self):
        input = Input()
        weight = input.promptMessage('weight','Kilo')
        height = input.promptMessage('height','Kilo')
    def __str__(self):
    
        return "Your weight Is {} and your height {}".format(weight,height)
       
class BMI:
    def __init__(self,jack):
        result = jack.hieght
    def __str__(self):
        return result

    
jack = Person()
bmiCal = BMI(jack)    

can someone please explain how I can get the height and weight attributes inside the Bmi object

I have had experience with java but in java I could easily pass one object inside another and use there attributes and even edit them

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You seem to have a typo right here:

class BMI:
    def __init__(self,jack):
        result = jack.hieght  <- here
    def __str__(self):
        return result

change it to jack.height.

Also, you should use the self keyword to bind result to your instance like so:

class BMI:
    def __init__(self,jack):
        self.result = jack.height
    def __str__(self):
        return self.result

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading