Age on Other Planets
Calculate how old you would be on each planet in the solar system based on their orbital periods.
常见问题
代码实现
from datetime import date
ORBITAL_PERIODS = {
"Mercury": 0.2408467,
"Venus": 0.6151972,
"Earth": 1.0,
"Mars": 1.8808158,
"Jupiter": 11.862615,
"Saturn": 29.447498,
"Uranus": 84.016846,
"Neptune": 164.79132,
}
def age_on_planets(birth_date: date, ref_date: date | None = None) -> dict:
if ref_date is None:
ref_date = date.today()
earth_years = (ref_date - birth_date).days / 365.25
return {
planet: round(earth_years / period, 2)
for planet, period in ORBITAL_PERIODS.items()
}
birth = date(1990, 6, 15)
ages = age_on_planets(birth)
for planet, age in ages.items():
print(f"{planet:8s}: {age:8.2f} years")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.