0

In this code, the constructor starts by asking for the name of a sun that is created from another class called Sun. Sun creates a sun object with 4 attributes: name, radius, mass, and temp. What I am trying to do in this solarsystem class is to calculate the total mass of all the planets plus the mass of the sun object, but I am quite confused on how I access the sun object's attributes I created through the Sun class. Haven't really found a good explanation yet

My code is below:

class SolarSystem:

    def __init__(self, asun):
        self.thesun = asun
        self.planets = []

    def addPlanet(self, aplanet):
        self.planets.append(aplanet)

    def showPlanet(self):
        for aplanet in self.planets:
            print(aplanet)

    def numPlanets(self):
        num = 0;
        for aplanet in self.planets:
            num = num + 1
        planets = num + 1
        print("There are %d in this solar system." % (planets))

    def totalMass(self):
        mass = 0
        sun = self.thesun
        sunMass = sun.mass
        for aplanet in self.planets:
            mass = mass + aplanet.mass
        totalMass = mass + sunMass
        print("The total mass of this solar system is %d" % (mass))

1 Answer 1

2

The following code works for me, I just had to change the print statement in totalMass() to use totalMass, not mass:

class SolarSystem:
    def __init__(self, asun):
        self.thesun = asun
        self.planets = []

    def addPlanet(self, aplanet):
        self.planets.append(aplanet)

    def showPlanet(self):
        for aplanet in self.planets:
            print(aplanet)

    def numPlanets(self):
        num = 0;
        for aplanet in self.planets:
            num = num + 1
        planets = num + 1
        print("There are %d in this solar system." % (planets))

    def totalMass(self):
        mass = 0
        sun = self.thesun
        sunMass = sun.mass
        for aplanet in self.planets:
            mass = mass + aplanet.mass
        totalMass = mass + sunMass
        print("The total mass of this solar system is %d" % (totalMass))

class Sun:
    def __init__(self, name, radius, mass, temp):
        self.name = name
        self.radius = radius
        self.mass = mass
        self.temp = temp

test_sun = Sun("test", 4, 100, 2)

test_solar_system = SolarSystem(test_sun)
test_solar_system.totalMass()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks the whole time I was thinking I was calling class objects the wrong way. I did find a better way to write the code. self.thesun.mass
Happy to help, remember to accept my answer so that people won't see this as an unanswered question when they're trying to help. Also, you can use the tilde key (`) to create single line code snippets like: self.thesun.mass which can be easier to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.