Reputation: 1
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.
Upvotes: 0
Views: 36
Reputation: 180391
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()))
Upvotes: 1