Your weight on each planet of the Solar System calculated in Python

Calculate your weight on different planets in Python

Hello, aspiring space cadets of Gdynia Space Academy! Ever wondered how much you'd weigh on Mars or Jupiter? Today, we'll use Python to build a program that calculates your weight on different planets. This skill might come in handy during your future space missions!

Before we blast off, let's understand the basics. Your weight changes on different planets because of gravity. But your mass stays the same. On Earth, we use weight and mass interchangeably, but in space, they're different!

Let's start coding our space weight calculator:

First, we'll create a dictionary of planets and their surface gravities:

planets = {
	"Mercury": 3.7,
	"Venus": 8.87,
	"Earth": 9.81,
	"Mars": 3.711,
	"Jupiter": 24.79,
	"Saturn": 10.44,
	"Uranus": 8.69,
	"Neptune": 11.15
}

These numbers represent the surface gravity of each planet in m/s².

What's a Dictionary?

In Python, a dictionary is a collection of key-value pairs. It's like a real-world dictionary where you have a word (the key) and its definition (the value). In our program, we use a dictionary to store planets (keys) and their corresponding surface gravities (values). This allows us to quickly look up the gravity of any planet we've included.

Dictionaries are very useful when you want to associate pieces of related information. They're written with curly braces {} and each key-value pair is separated by a comma. The key and value in each pair are separated by a colon.

Next, let's create a function to calculate weight:

def calculate_weight(mass, gravity):
	return mass * gravity

This function multiplies mass by gravity to get weight.

Now, let's get input from the user:

mass = float(input("Enter your mass in kg: "))
print("Available planets:")
for planet in planets:
	print(planet)
chosen_planet = input("Enter the planet you want to calculate your weight on: ")

We ask for the user's mass and show a list of available planets.

The user then chooses a planet. Time to calculate and display the weight:

if chosen_planet in planets:
	gravity = planets[chosen_planet]
	weight = calculate_weight(mass, gravity)
	print(f"Your weight on {chosen_planet} would be {weight:.2f} N")
else:
	print("Planet not found in our database. Are you sure you spelled it correctly?")

This code checks if the chosen planet is in our dictionary. If it is, it calculates the weight and displays it. If not, it shows an error message. Let's put it all together.

Note that our program calculates weight in Newtons (N), not kilograms (kg). This is because weight is actually a force - the force of gravity acting on an object's mass. While your mass stays constant, your weight changes depending on the gravity of the planet you're on.

And there you have it, future space explorers! You've just created a program that calculates weight on different planets. This is just the beginning of your journey at Gdynia Space Academy. Who knows? One day, you might use this knowledge on real interplanetary missions!

Remember, in space exploration, every calculation matters. Keep practicing your Python skills, and soon you'll be ready for more advanced space simulations. The stars are waiting for you!

The complete code for our space weight calculator is in the code tab.

Now that you know how to calculate your weight in newtons on different planets, how about an additional challenge? Try modifying the program to display the weight in kilograms as well.

Here are some tips:

  1. Weight in kilograms is simply the weight in newtons divided by the gravity on Earth's surface (approximately 9.80665 m/s²).
  2. You can add a new function to perform the conversion.
planets = {
	"Mercury": 3.7,
	"Venus": 8.87,
	"Earth": 9.81,
	"Mars": 3.711,
	"Jupiter": 24.79,
	"Saturn": 10.44,
	"Uranus": 8.69,
	"Neptune": 11.15
}

def calculate_weight(mass, gravity):
	return mass * gravity

mass = float(input("Enter your mass in kg: "))
print("Available planets:")
for planet in planets:
	print(planet)
chosen_planet = input("Enter the planet you want to calculate your weight on: ")

if chosen_planet in planets:
	gravity = planets[chosen_planet]
	weight = calculate_weight(mass, gravity)
	print(f"Your weight on {chosen_planet} would be {weight:.2f} N")
else:
	print("Planet not found in our database. Are you sure you spelled it correctly?")

Discussion

Questions or ideas about this article? Join the discussion.

Log in to ask and reply.

Explore other topics

A robot standing next to a Japanese restaurant, the cover image for an article on whether learning Python is still worth it in the age of AI

Is It Worth Learning Python in the Age of AI? (2026)

AI now writes a large share of new code, so is learning Python still worth your time? Yes, but for a different reason than ten years ago. Using job data, developer surveys and recent studies on learning with AI, we look at what changed and where Python’s

Beginner’s guide to Python functions: code example with def, parameters and return

Python Functions for Beginners

Learn to write your own Python functions, from your first def to return values, default parameters and scope, with runnable examples, common mistakes and six practice projects.

Converting decimal numbers to Rocky's base-6 Eridian number system in Python

Base 6 Number System in Python

In Project Hail Mary, the alien engineer Rocky counts in base 6 because he has two hands with three fingers each. In this article, you'll build Python programs that convert between decimal, base-6, and Eridian notation.

Python if, elif and else decisions explained with space mission examples

Python If/Else for Beginners

Learn Python if, elif, and else with clear, space-themed examples and hands-on exercises. Perfect for beginners who want to chart the control flow of their programs.