python - How do I add different amounts to one inputted variable? -


i have nested list names, longest , shortest distances sun, , time frame (in weeks) @ planets away 01/01/16. variable number of weeks user inputs, because planets start on '-x' axis default, have +/- necessary amount of time users time in order position them correctly.

week=int(input("how many weeks see 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 @ moment, trying create main function variables (which include user's "week" input) , keep constants separate. brings me problem of defining planets shown above without variable in them. don't understand how have '+1.5' in planets definition , add users input separately in functions section.

an example of how list used in function given below, there approximately 8 functions using different combinations of information 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 executed in main function so:

def main():      week=int(input("how many weeks see solar system's future?   "))      name, max, min, time in planetdata:         mapplanet(max, min, time)         drawplanet(name, max, min, time) 

what can in case define function store input data each planet through closure , calculate 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 use function define each planet (the code below simplified version of code planetdata 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)], ]  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 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -