0

I am trying to create a really simple Python program where I can input a planet by its name and have the approximate distance stated. Currently, I have the seven planets listed and equal to their distances right below that eval function.

This is what the code looks like currently:

def = def Planet_Calculations():
    Planet = exec(input("What planet are you trying to calculate the distance for? (Note: Pluto is no longer a planet!) "))
    Mercury = 56,974,146
    Venus = 25,724,767 
    Earth = 0 
    Mars = 48,678,219
    Jupiter = 390,674,710
    Saturn = 792,248,270
    Uranus = 1,692,662,530
    Neptune = 2,703,959,960

print("The distance to the specified planet is approxametly:" , Planet, "million miles from Earth." )

Planet_Calculations()

When I try to type in a planet such as "Mars" into the eval, I have no idea how to actually have the program input its distance into the print function further down. I would greatly appreciate any type of feedback or help.

0

1 Answer 1

1

Use a dict mapping planets to the tuples::

def planet_calculations():
    planet = input("What planet are you trying to calculate the distance for? (Note: Pluto is no longer a planet!) ")
    planets = {'Mercury': (56, 974, 146), 'Neptune': (2, 703, 959, 960), 'Jupiter': (390, 674, 710),
               'Uranus': (1, 692, 662, 530),
               'Mars': (48, 678, 219), 'Earth': 0, 'Venus': (25, 724, 767), 'Saturn': (792, 248, 270)}
    print("The distance to the specified planet is approxametly: {} million miles from Earth."
          .format(planets[planet])

If you want the output like "56,974,146" store the values as strings and not tuples.

Also if you want to use the value outside the function, you need to return it:

def planet_calculations():
    planet = input("What planet are you trying to calculate the distance for? (Note: Pluto is no longer a planet!) ")
    planets = {'Mercury': (56, 974, 146), 'Neptune': (2, 703, 959, 960), 'Jupiter': (390, 674, 710),
               'Uranus': (1, 692, 662, 530),
               'Mars': (48, 678, 219), 'Earth': 0, 'Venus': (25, 724, 767), 'Saturn': (792, 248, 270)}
    return planets[planet]
print("The distance to the specified planet is approxametly: {} million miles from Earth."
        .format(planet_calculations()))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help Padraic!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.