Using a class to represent the planet and its gravity, develop a program that outputs the user's weight on different planets.
class Planet:
data = {"MOON": .165,
"VENUS": .906,
"JUPITER": 2.442,
"NEPTUNE": 1.131,
"SATURN": 1.065,
"MARS": .394,
"MERCURY": .375,
"SUN": 27.85,
"EARTH": 1.0}
def __init__(self, name=""):
if name not in Planet.data:
self.name = "EARTH"
self.gravity = Planet.data["EARTH"]
else:
self.name = name
self.gravity = Planet.data[name]
def convertWeight(self, earthWeight):
weight = earthWeight * self.gravity
print(f"weight on {self.name} is {weight:.02f}")
jupiter = Planet("JUPITER")
jupiter.convertWeight(76)
Comments
Leave a comment