import random
class StarSystem:
def __init__(self, name, star_type, num_planets):
self.name = name
self.star_type = star_type
self.num_planets = num_planets
self.planets = []
def generate_planets(self):
for i in range(self.num_planets):
planet_name = f"{self.name}-p{i+1}"
planet_type = random.choice(["Rocky", "Gas giant", "Ice giant"])
planet_orbit = random.uniform(0.3, 30) # Orbit distance from star in AU
planet_size = random.uniform(0.1, 10) # Planet size in Earth radii
planet_moons = random.randint(0, 10) # Number of moons
planet = {
"name": planet_name,
"type": planet_type,
"orbit_distance": planet_orbit,
"size": planet_size,
"moons": planet_moons
}
self.planets.append(planet)
def display_system_info(self):
print(f"Star System: {self.name} ({self.star_type})")
print(f"Number of planets: {self.num_planets}")
for planet in self.planets:
print(f"\nPlanet: {planet['name']}")
print(f"Type: {planet['type']}")
print(f"Orbit distance: {planet['orbit_distance']} AU")
print(f"Size: {planet['size']} Earth radii")
print(f"Moons: {planet['moons']}")
# Generate a sample star system
name = "Solar System"
star_type = "G-type main-sequence star"
num_planets = 8
solar_system = StarSystem(name, star_type, num_planets)
solar_system.generate_planets()
solar_system.display_system_info()
This code defines a StarSystem class that represents a star system with a name, star type, number of planets, and a list of planet objects. The generate_planets method randomly generates planet attributes such as type, orbit distance, size, and number of moons, and adds them to the list of planets in the star system. The display_system_info method prints out the information about the star system and its planets.