# PlanetsDictionary.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-28 File created. # 2022-12-27 Converted to Python 3. # This is another version of program Planets.py. # In this version Planet objects are stored in a Python dictionary. # The main program is thus shorter. Class Planet is the same as in # Planets.py class Planet : def __init__( self, given_planet_name, given_mean_distance_from_sun, given_circulation_time_around_sun, given_rotation_time_around_own_axis, given_equatorial_radius_in_kilometers, given_mass_compared_to_earth, given_number_of_moons ) : self.planet_name = given_planet_name self.mean_distance_from_sun = given_mean_distance_from_sun self.circulation_time_around_sun = given_circulation_time_around_sun self.rotation_time_around_own_axis = \ given_rotation_time_around_own_axis self.equatorial_radius_in_kilometers = \ given_equatorial_radius_in_kilometers self.mass_compared_to_earth = given_mass_compared_to_earth self.number_of_moons = given_number_of_moons def get_planet_name( self ) : return self.planet_name def print_planet_data( self ) : print( "\n %s orbits the sun in average distance of %.0f kilometers." %\ ( self.planet_name, ( self.mean_distance_from_sun * 149500000 )), end="" ) print( "\n It takes %f years for %s to go around the sun once." % \ ( self.circulation_time_around_sun, self.planet_name ), end="" ) print( "\n The radius of %s is %d kilometers." % \ ( self.planet_name, self.equatorial_radius_in_kilometers ) ) # The main program begins. # Much of the information given in the following table is # relative to the information concerning the Earth. E.g., # Pluto is 39.507 times further from the sun than the Earth. # The data concerning the number of moons may be old. planet_dictionary = {} # Create an empty dictionary planet_dictionary[ "mercury" ] = Planet( "Mercury", 0.387, 0.241, 58.815, 2433, 0.05, 0 ) planet_dictionary[ "venus" ] = Planet( "Venus", 0.723, 0.615, 224.588, 6053, 0.82, 0 ) planet_dictionary[ "earth" ] = Planet( "Earth", 1.000, 1.000, 1.000, 6379, 1.00, 1 ) planet_dictionary[ "mars" ] = Planet( "Mars", 1.523, 1.881, 1.029, 3386, 0.11, 2 ) planet_dictionary[ "jupiter" ] = Planet( "Jupiter", 5.203, 11.861, 0.411, 71370, 317.93, 12 ) planet_dictionary[ "saturn" ] = Planet( "Saturn", 9.541, 29.457, 0.428, 60369, 95.07, 10 ) planet_dictionary[ "uranus" ] = Planet( "Uranus", 19.190, 84.001, 0.450, 24045, 14.52, 5 ) planet_dictionary[ "neptune" ] = Planet( "Neptune", 30.086, 164.784, 0.657, 22716, 17.18, 2 ) planet_dictionary[ "pluto" ] = Planet( "Pluto", 39.507, 248.35, 6.410, 5700, 0.18, 0 ) print( "\n This program can give you information about the" \ "\n planets in our solar system. Give a planet name: ", end="" ) planet_name_from_user = input().lower() if planet_name_from_user in planet_dictionary : planet_dictionary[ planet_name_from_user ].print_planet_data() else : print( "\n Sorry, could not find information on \"%s\"." % \ planet_name_from_user ) print( "" )