2012-11-25 42 views
0

我需要的問題有所幫助。例如,我有以下字典planetspython3遞歸的正確性我的功能

{'Mercury': {'Orbital Radius': '38001200', 'Radius': '243900.7', 
'Period': '87.9691'}, 'Ariel': {'Orbital Radius': '8595000', 'Radius': 
'60000', 'Period': '2.520379'}, 'Sun': {'Satellites': 
'Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris', 
'Orbital Radius': '0', 'Radius': '20890260'}, 'Phobos': {'Orbital 
Radius': '3623500.6', 'Radius': '200000', 'Period': '0.31891023'}, 
'Deimos': {'Orbital Radius': '8346000', 'Radius': '200000.2', 
'Period': '1.26244'}, 'Mars': {'Satellites': 'Phobos,Deimos', 'Orbital 
Radius': '106669000', 'Radius': '339600.2', 'Period': '686.971'}, 
'Rhea': {'Orbital Radius': '24000000', 'Radius': '75000', 'Period': 
'4.5'}, 'Oberon': {'Orbital Radius': '26235000', 'Radius': '75000', 
'Period': '13.463'}, 'Europa': {'Orbital Radius': '36486200', 
'Radius': '156000.8', 'Period': '3.551181'}, 'Tethys': {'Orbital 
Radius': '13706000', 'Radius': '50000', 'Period': '1.9'}, 'Miranda': 
{'Orbital Radius': '5822550', 'Radius': '23500', 'Period': '1.413'}, 
'Saturn': {'Satellites': 
'Mimas,Enceladus,Tethys,Dione,Rhea,Titan,Iapetus', 'Orbital Radius': 
'353572956', 'Radius': '6026800', 'Period': '10759.22'}, 'Uranus': 
{'Satellites': 'Puck,Miranda,Ariel,Umbriel,Titania,Oberon', 'Orbital 
Radius': '453572956', 'Radius': '2555900', 'Period': '30799'}, 
'Neptune': {'Satellites': 'Triton', 'Orbital Radius': '550000000', 
'Radius': '2476400', 'Period': '60190'}, 'Titania': {'Orbital Radius': 
'19575000', 'Radius': '75000', 'Period': '8.7058'}, 'Enceladus': 
{'Orbital Radius': '10706000', 'Radius': '25000', 'Period': '1.4'}, 
'Venus': {'Orbital Radius': '57477000', 'Radius': '605100.8', 
'Period': '224.698'}, 'Moon': {'Orbital Radius': '18128500', 'Radius': 
'173700.10', 'Period': '27.321582'}, 'Triton': {'Orbital Radius': 
'40000000', 'Radius': '135300', 'Period': '-5.8'}, 'Ceres': {'Orbital 
Radius': '130995855', 'Radius': '48700', 'Period': '1679.67'}, 
'Mimas': {'Orbital Radius': '8433396', 'Radius': '20600', 'Period': 
'0.9'}, 'Titan': {'Orbital Radius': '50706000', 'Radius': '257600', 
'Period': '15.945'}, 'Ganymede': {'Orbital Radius': '47160000', 
'Radius': '263400', 'Period': '7.15455296'}, 'Umbriel': {'Orbital 
Radius': '11983500', 'Radius': '60000', 'Period': '4.144177'}, 
'Callisto': {'Orbital Radius': '69700000', 'Radius': '241000', 
'Period': '16.6890184'}, 'Jupiter': {'Satellites': 
'Io,Europa,Ganymede,Callisto', 'Orbital Radius': '210573600', 
'Radius': '7149200', 'Period': '4332.59'}, 'Io': {'Orbital Radius': 
'22000000', 'Radius': '182100.3', 'Period': '1.7691377186'}, 'Earth': 
{'Satellites': 'Moon', 'Orbital Radius': '77098290', 'Radius': 
'637100.0', 'Period': '365.256363004'}, 'Dione': {'Orbital Radius': 
'17106000', 'Radius': '56000', 'Period': '2.7'}, 'Iapetus': {'Orbital 
Radius': '72285891', 'Radius': '75000', 'Period': '79'}} 

這是我的代碼太陽能動畫。我的問題是,我的代碼是否不斷去檢查'衛星'的每個'對象',然後打印它們,如果它們存在?它是遞歸的嗎?我一整天都在努力工作,讓它檢查,然後打印!

#code for solar animation 
#t = period 
#x,y = rootobject coordinates 

print("flush false") 
scale = 600/max([planets[key]['Orbital Radius'] for key in planets]) 
t = 0 
x = 400 
y = 300 
print("fillcircle",400,300,planets['Sun']['Radius']*scale) 
print("text ", "\"Sun\"",x + planets['Sun']['Radius']*scale,y) 


def satellites(planets): 
    for 'Satellites' in planets: 
     if 'Satellites' not in 'Object': 
      return 
     else: 
      print("refresh") 
      print("colour 0 0 0") 
      print("clear") 
      print("colour 255 255 255") 
      r_sat = planets['Object']['Satellites']['Orbital Radius']*scale 
      print("circle",x,y,r_sat) 
      r_satX = x + math.sin(t*2*math.pi/planets['Object']['Satellites']['Period'])*r_sat 
      r_satY = y + math.cos(t*2*math.pi/planets['Object']['Satellites']['Period'])*r_sat 
      print("fillcircle",r_satX,r_satY,planets['Object']['Satellites']['Radius']*scale) 
      print("text ",['Object']['Satellites'],r_satX + planets['Object']['Satellites']['Radius']*scale,r_satY) 
      t += 0.02 

system = satellites(planets) 
print(system) 

我已經接近我的極限了,我試過了我能想到的所有東西,但我需要幫助!

+2

您使用字符串文本而不是變量名:它應該是'在planets'衛星和'如果沒有衛星在對象中。沒有看完其餘的,祝你好運:) –

+0

好的!謝謝 !我想我明白了。 – Romulus

回答

2

1.我的問題是,沒有我的代碼不斷地去檢查的「衛星」每一個「對象」,然後如果存在打印出來?

不,你的functionsatellites()它是無效的python。這是相當與它的幾個錯誤,但我可以看到你標題在正確的方向,排序

for 'Satellites' in planets: 
    if 'Satellites' not in 'Object': 
     return 

'Satellites'只是一個字符串,所以for 'string' in 'iterable object'是無效的蟒蛇。

if 'Satellites' not in 'Object':有同樣的問題,因爲之前的路線,'Satellites'永遠是在'Object'

我相信這是你想要做什麼:

for key in planets.keys():    # For all keys in the outer dic 
    if 'Satellites' not in planets[key]: # Does inner dic have key 'Satellites' 
     return 

key哪裏是採用包含在planets一旦輪循環中的每個值的變量(不是字符串)。所以第一次key的值是'Mecury',第二次key的值是'Sun'等等。然後我們看內部字典planets[key]並檢查該字典是否有密鑰'Satellites'

然後使用planets[key]['Satellites']而不是planets['Object']['Satellites']作爲密鑰'Object'從不存在(查看輸入文件)。

2.它是遞歸的?

不,你functionsatellites()從來不打電話itselfs,重新閱讀遞歸我的回答:python3 recursion animation in QuickDraw

+0

是的,這正是我想要得到的!感謝您清理它!我多次閱讀。這是否意味着我的'如果衛星不在行星[關鍵]:'不是基本情況? – Romulus

+0

你是在正確的軌道上,如果沒有'Satellites'留下來打印,然後返回。 –

+0

是啊,這是我的想法! – Romulus