You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何定义Planet类并适配行星数据代码、实现天数转行星年方法?

Solution for Planet Class Implementation

Step 1: Define the Planet Class

First, create a Planet class that encapsulates all planet attributes and includes the required conversion method:

class Planet:
    def __init__(self, name, sun_dist, revolution_days, sats_num):
        # Initialize core planet attributes
        self.name = name
        self.sun_dist = sun_dist
        self.revolution_days = revolution_days
        self.sats_num = sats_num
    
    def __str__(self):
        # Return formatted data dictionary when printing the instance
        return str({
            'Planet': self.name,
            'Distance from the Sun': self.sun_dist,
            'Duration of the year': self.revolution_days,
            'Number of satellites': self.sats_num
        })
    
    def days_to_years(self, days):
        # Convert Earth days to the planet's local year count
        return days / self.revolution_days

Step 2: Batch Create Planet Instances

Use your existing data lists to generate all planet instances, and assign them to global variables for direct access (like Pluto):

# Your original planet data
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
sunDist = [57.9, 108.2, 149.6, 227.9, 778.3, 1427, 2870, 4497, 5900]
revolut = [88, 224.7, 365.2, 687, 4328, 10752, 651160, 1020707, 1338090]
satsNum = [0, 0, 1, 2, 63, 56, 27, 13, 3]

# Store instances in a dictionary for quick lookup
planet_instances = {}

# Generate instances and assign to global variables
for name, dist, rev_days, sats in zip(planets, sunDist, revolut, satsNum):
    new_planet = Planet(name, dist, rev_days, sats)
    planet_instances[name] = new_planet
    globals()[name] = new_planet  # Enables direct access like Pluto.name

Step 3: Adapt Existing Code

Modified Bubble Sort (By Satellite Count)

Update your bubble sort to work with Planet instances instead of raw strings:

def bubble_sort_by_satellites(planet_list):
    n = len(planet_list)
    for i in range(n):
        swapped = False
        for j in range(n - i - 1):
            if planet_list[j].sats_num > planet_list[j+1].sats_num:
                planet_list[j], planet_list[j+1] = planet_list[j+1], planet_list[j]
                swapped = True
        if not swapped:
            break
    return planet_list

# Usage example
sorted_planets = bubble_sort_by_satellites(list(planet_instances.values()))
for p in sorted_planets:
    print(f"{p.name}: {p.sats_num} satellites")

Updated planetData Function

Modify the function to return the data dictionary using the instance:

def planetData(pName):
    planet = planet_instances.get(pName)
    if planet:
        return {
            'Planet': planet.name,
            'Distance from the Sun': planet.sun_dist,
            'Duration of the year': planet.revolution_days,
            'Number of satellites': planet.sats_num
        }
    return None  # Return None if planet not found

Step 4: Test the Features

  • Access direct attributes: print(Pluto.name) outputs Pluto
  • Print instance data: print(Pluto) outputs the full data dictionary
  • Convert days to years: print(Mars.days_to_years(687)) outputs 1.0

内容的提问来源于stack exchange,提问作者Coppertank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:40:27