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))