Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Project

Project - Moon Measurer with Dictionaries

Remember when we worked on the moon measurer, and calculated a lot of information about how much people weigh on different planets? We’re back at it!

The tests for this one don’t work yet, sorry! Soon, soon. You’re welcome to work on it on your own, though. In fact, I don’t know if anything about this one works. Congratulations on being ahead of the curve.

Last time we had bet our friend whether we could pick them up on different planets, but this time they’re being much more demanding. But first, A little review:

Your weight on different planets1 depends on the surface gravity of that planet. According to the surface gravity page on Wikipedia, the Earth has a surface gravity of 1g, while the Moon has a surface gravity of 0.1654g. This means if something weighs 100kg on the Earth, it would weigh 16.54kg on the Moon.

Last time we had to see how much our 80kg friend weighed on other planets[1], and we did a lot of calculations based on this list:

Planet Surface Gravity
Venus 0.904g
Earth 1g
Mars 0.376g
Ceres 0.0275g
Jupiter 2.53g
Europa 0.134g
Encelaedus 0.0113g
HD 40307g 1.420g

Remember how dictionaries are good at storing the same information for many things? We’re using that ability in this project - we’re starting with a dictionary called gravities that saves the surface gravity for each planet.

This time, we want to create a new dictionary of their weight on each planet, and print our friend’s weight on each planet. Be sure to use round to round to the nearest 2 decimal points, just like last time.

gravities = {
    'Venus': 0.904,
    'Earth': 1,
    'Mars': 0.376,
    'Ceres': 0.0275,
    'Jupiter': 2.53,
    'Europa': 0.134,
    'Enceladaedus': 0.0113,
    'HD 40307g': 1.420
}

weights = {}
# weights is an empty dictionary, with no keys or values (yet).
# Let's try to do this without putting anything between the {}.
# Instead, add new keys and values one-by-one in the lines below

# After you save the information, print out the weight for each planet
  • Hint: Use print to print out the name and the weight
  • Hint: We can print strings and numbers together without trouble if we separate with commas, e.g. print("Words words", 30, "more words")
  • Hint: Since the Moon has a surface gravity of 0.1654g and Earth has a surface gravity of 1g, we can convert from Earth weight to Moon weight by multiplying the Earth weight by 0.1654
  • Hint: round(weight * 0.1654, 2) would give us a rounded weight on the moon
  • Hint: We want to use the dictionary this time, though. We can read that Venus is 0.904, but how do we get that out of the gravities dictionary?
  • Hint: Use gravities['Venus'] to get the surface gravity for Venus.
  • Hint: To convert 80kg on Earth to Venus weight, you would multiply like 80 * gravities['Venus']. But instead of 80, you’ll want to use a variable.
  • Hint: If we want to do the calculation and display all in one line, we can do something like print("On Venus he weighs", round(weight * gravities['Venus'], 2)). If that seems too complicated, we can also save round(weight * gravities['Venus'], 2) into its own variable named venus_weight, and print(and have theprint` on another line!

[1] Planet or other body in space. Moons are cool, dwarf planets are cool, planets from far away are cool. I don’t want to hear you complaining.