I currently have a nested list with the names, longest and shortest distances from the sun, and the time frame (in weeks) at which those planets are away from 01/01/16. The variable is the number of weeks the user inputs, however because all the planets initially start on the '-x' axis by default, I have +/- the necessary amount of time from the users time in order to position them correctly.
Week=int(input("How many weeks would you like to see into the Solar System's future? "))
TimeFormat=365.25*60*60*24
PlanetData = [
['Mercury', 69.8, 46.0, (Week+1.5)/52 * TimeFormat],
['Venus', 108.9, 107.5, (Week-9)/52 * TimeFormat],
['Earth', 152.1, 147.1, (Week-1.5)/52 * TimeFormat],
['Mars', 249.2, 206.7, (Week-21)/52 * TimeFormat],
["Halley's Comet",5250, 87.7, (Week+1.54e3)/52 * TimeFormat],
]
This works fine at the moment, however I am trying to create a main function with all variables (which would include the user's "week" input) and then keep constants separate. This brings me to the problem of defining the Planets as shown above without a variable in them. I don't understand how to have just '+1.5' in the planets definition and then add the users input separately in the functions section.
An example of how this list is used in a function is given below, however there are approximately 8 functions using different combinations of the information for each planet.
def MapPlanet(Max, Min, Time):
SCALE = 1e9
theta, r = SolveOrbit(Max * SCALE, Min * SCALE, Time)
x = -r * cos(theta) / SCALE
y = r * sin(theta) / SCALE
return x, y
def DrawPlanet(Name, Max, Min, Time):
x, y = MapPlanet(Max, Min, Time)
Planet = Circle((x, y), 8)
plt.figure(0).add_subplot(111, aspect='equal').add_artist(Planet)
plt.annotate(Name, xy=((x+5),y),color='red')
This would then be executed in the main function like so:
def Main():
Week=int(input("How many weeks would you like to see into the Solar System's future? "))
for Name, Max, Min, Time in PlanetData:
MapPlanet(Max, Min, Time)
DrawPlanet(Name, Max, Min, Time)
What you can do in this case is to define a function that will store your input data for each planet through closure and calculate the proper value:
TimeFormat=365.25*60*60*24
def planet_time(data):
def value(Week):
return (Week+data)/52 * TimeFormat
return value(Week)
You can then use this function to define each planet (the code below is a simplified version of your code but PlanetData is complete):
Week = 4
PlanetData = [
['Mercury', 69.8, 46.0, planet_time(1.5)],
['Venus', 108.9, 107.5, planet_time(-9.0)],
['Earth', 152.1, 147.1, planet_time(-1.5)],
['Mars', 249.2, 206.7, planet_time(21.0)],
["Halley's Comet",5250, 87.7, planet_time(1.54e3)],
]
for Name, Max, Min, Time in PlanetData:
print("{}, {}".format(Name, Time))
This code prints:
Mercury, 3337823.07692
Venus, -3034384.61538
Earth, 1517192.30769
Mars, 15171923.0769
Halley's Comet, 937017969.231