1
我目前有一個名字,距離太陽最遠和最短距離的嵌套列表,以及這些行星遠離01/01/16的時間範圍(以星期爲單位)。這個變量是用戶輸入的星期數,但是因爲所有行星最初默認都是在'-x'軸上開始的,所以爲了正確定位它們,從用戶時間開始必須有+/-時間。如何爲一個輸入變量添加不同的金額?
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],
]
也能正常工作的時刻,但我想創建的所有變量主要功能(其中包括用戶的「周」輸入),然後保持常量分開。這給我帶來了如上所示定義行星的問題,但沒有變量。我不明白如何在行星定義中使用'+1.5',然後在函數部分單獨添加用戶輸入。
下面給出了這個列表在函數中如何使用的一個例子,但是大約有8個函數使用每個星球的不同信息組合。
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')
這然後在主函數中執行如下所示:
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)