Answer to Question #325116 in Python for junior

Question #325116

Using a class to represent the planet and its gravity, develop a program that outputs the user's weight on different planets.


  • The class's constructor should allow a planet to specified as a capitalized string. If the string is not a planet's name or is not capitalized, then the planet of EARTH should be assumed as the default value.
  • The default constructor for the class will create an object representing Earth. The class has a function called "convertWeight" that takes in a weight on earth as an argument and returns the name of the planet as well as the converted value reflecting weight on the given planet to console.
  • The program should output an error message and reprompt for input if the user does not input a correct planet name that is also capitalized.




1
Expert's answer
2022-04-07T04:04:52-0400
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS