how to make a function recursive

Posted by tom smith on Stack Overflow See other posts from Stack Overflow or by tom smith
Published on 2012-11-25T05:01:58Z Indexed on 2012/11/25 5:03 UTC
Read the original article Hit count: 556

Filed under:
|
|
|

i have this huge function and i am wondering how to make it recursive. i have the base case which should never come true, so it should always go to else and keep calling itself with the variable t increases. any help would be great thanks

def draw(x, y, t, planets):
    if 'Satellites' in planets["Moon"]:
        print ("fillcircle", x, y, planets["Moon"]['Radius']*scale)
    else:
        while True:
            print("refresh")
            print("colour 0 0 0")
            print("clear")
            print("colour 255 255 255")
            print("fillcircle",x,y,planets['Sun']['Radius']*scale)
            print("text ", "\"Sun\"",x+planets['Sun']['Radius']*scale,y)
            if "Mercury" in planets:
                r_Mercury=planets['Mercury']['Orbital Radius']*scale;
                print("circle",x,y,r_Mercury)
                r_Xmer=x+math.sin(t*2*math.pi/planets['Mercury']['Period'])*r_Mercury
                r_Ymer=y+math.cos(t*2*math.pi/planets['Mercury']['Period'])*r_Mercury
                print("fillcircle",r_Xmer,r_Ymer,3)
                print("text ", "\"Mercury\"",r_Xmer+planets['Mercury']['Radius']*scale,r_Ymer)
            if "Venus" in planets:
                r_Venus=planets['Venus']['Orbital Radius']*scale;
                print("circle",x,y,r_Venus)
                r_Xven=x+math.sin(t*2*math.pi/planets['Venus']['Period'])*r_Venus
                r_Yven=y+math.cos(t*2*math.pi/planets['Venus']['Period'])*r_Venus
                print("fillcircle",r_Xven,r_Yven,3)
                print("text ", "\"Venus\"",r_Xven+planets['Venus']['Radius']*scale,r_Yven)
            if "Earth" in planets:
                r_Earth=planets['Earth']['Orbital Radius']*scale;
                print("circle",x,y,r_Earth)
                r_Xe=x+math.sin(t*2*math.pi/planets['Earth']['Period'])*r_Earth
                r_Ye=y+math.cos(t*2*math.pi/planets['Earth']['Period'])*r_Earth
                print("fillcircle",r_Xe,r_Ye,3)
                print("text ", "\"Earth\"",r_Xe+planets['Earth']['Radius']*scale,r_Ye)
            if "Moon" in planets:
                r_Moon=planets['Moon']['Orbital Radius']*scale;
                print("circle",r_Xe,r_Ye,r_Moon)
                r_Xm=r_Xe+math.sin(t*2*math.pi/planets['Moon']['Period'])*r_Moon
                r_Ym=r_Ye+math.cos(t*2*math.pi/planets['Moon']['Period'])*r_Moon
                print("fillcircle",r_Xm,r_Ym,3)
                print("text ", "\"Moon\"",r_Xm+planets['Moon']['Radius']*scale,r_Ym)
            if "Mars" in planets:
                r_Mars=planets['Mars']['Orbital Radius']*scale;
                print("circle",x,y,r_Mars)
                r_Xmar=x+math.sin(t*2*math.pi/planets['Mars']['Period'])*r_Mars
                r_Ymar=y+math.cos(t*2*math.pi/planets['Mars']['Period'])*r_Mars
                print("fillcircle",r_Xmar,r_Ymar,3)
                print("text ", "\"Mars\"",r_Xmar+planets['Mars']['Radius']*scale,r_Ymar)
            if "Phobos" in planets:
                r_Phobos=planets['Phobos']['Orbital Radius']*scale;
                print("circle",r_Xmar,r_Ymar,r_Phobos)
                r_Xpho=r_Xmar+math.sin(t*2*math.pi/planets['Phobos']['Period'])*r_Phobos
                r_Ypho=r_Ymar+math.cos(t*2*math.pi/planets['Phobos']['Period'])*r_Phobos
                print("fillcircle",r_Xpho,r_Ypho,3)
                print("text ", "\"Phobos\"",r_Xpho+planets['Phobos']['Radius']*scale,r_Ypho)
            if "Deimos" in planets:
                r_Deimos=planets['Deimos']['Orbital Radius']*scale;
                print("circle",r_Xmar,r_Ymar,r_Deimos)
                r_Xdei=r_Xmar+math.sin(t*2*math.pi/planets['Deimos']['Period'])*r_Deimos
                r_Ydei=r_Ymar+math.cos(t*2*math.pi/planets['Deimos']['Period'])*r_Deimos
                print("fillcircle",r_Xdei,r_Ydei,3)
                print("text ", "\"Deimos\"",r_Xpho+planets['Deimos']['Radius']*scale,r_Ydei)
            if "Ceres" in planets:
                r_Ceres=planets['Ceres']['Orbital Radius']*scale;
                print("circle",x,y,r_Ceres)
                r_Xcer=x+math.sin(t*2*math.pi/planets['Ceres']['Period'])*r_Ceres
                r_Ycer=y+math.cos(t*2*math.pi/planets['Ceres']['Period'])*r_Ceres
                print("fillcircle",r_Xcer,r_Ycer,3)
                print("text ", "\"Ceres\"",r_Xcer+planets['Ceres']['Radius']*scale,r_Ycer)
            if "Jupiter" in planets:
                r_Jupiter=planets['Jupiter']['Orbital Radius']*scale;
                print("circle",x,y,r_Jupiter)
                r_Xjup=x+math.sin(t*2*math.pi/planets['Jupiter']['Period'])*r_Jupiter
                r_Yjup=y+math.cos(t*2*math.pi/planets['Jupiter']['Period'])*r_Jupiter
                print("fillcircle",r_Xjup,r_Yjup,3)
                print("text ", "\"Jupiter\"",r_Xjup+planets['Jupiter']['Radius']*scale,r_Yjup)
            if "Io" in planets:
                r_Io=planets['Io']['Orbital Radius']*scale;
                print("circle",r_Xjup,r_Yjup,r_Io)
                r_Xio=r_Xjup+math.sin(t*2*math.pi/planets['Io']['Period'])*r_Io
                r_Yio=r_Yjup+math.cos(t*2*math.pi/planets['Io']['Period'])*r_Io
                print("fillcircle",r_Xio,r_Yio,3)
                print("text ", "\"Io\"",r_Xio+planets['Io']['Radius']*scale,r_Yio)
            if "Europa" in planets:
                r_Europa=planets['Europa']['Orbital Radius']*scale;
                print("circle",r_Xjup,r_Yjup,r_Europa)
                r_Xeur=r_Xjup+math.sin(t*2*math.pi/planets['Europa']['Period'])*r_Europa
                r_Yeur=r_Yjup+math.cos(t*2*math.pi/planets['Europa']['Period'])*r_Europa
                print("fillcircle",r_Xeur,r_Yeur,3)
                print("text ", "\"Europa\"",r_Xeur+planets['Europa']['Radius']*scale,r_Yeur)
            if "Ganymede" in planets:
                r_Ganymede=planets['Ganymede']['Orbital Radius']*scale;
                print("circle",r_Xjup,r_Yjup,r_Ganymede)
                r_Xgan=r_Xjup+math.sin(t*2*math.pi/planets['Ganymede']['Period'])*r_Ganymede
                r_Ygan=r_Yjup+math.cos(t*2*math.pi/planets['Ganymede']['Period'])*r_Ganymede
                print("fillcircle",r_Xgan,r_Ygan,3)
                print("text ", "\"Ganymede\"",r_Xgan+planets['Ganymede']['Radius']*scale,r_Ygan)
            if "Callisto" in planets:
                r_Callisto=planets['Callisto']['Orbital Radius']*scale;
                print("circle",r_Xjup,r_Yjup,r_Callisto)
                r_Xcal=r_Xjup+math.sin(t*2*math.pi/planets['Callisto']['Period'])*r_Callisto
                r_Ycal=r_Yjup+math.cos(t*2*math.pi/planets['Callisto']['Period'])*r_Callisto
                print("fillcircle",r_Xcal,r_Ycal,3)
                print("text ", "\"Callisto\"",r_Xcal+planets['Callisto']['Radius']*scale,r_Ycal)
            if "Saturn" in planets:
                r_Saturn=planets['Saturn']['Orbital Radius']*scale;
                print("circle",x,y,r_Saturn)
                r_Xsat=x+math.sin(t*2*math.pi/planets['Saturn']['Period'])*r_Saturn
                r_Ysat=y+math.cos(t*2*math.pi/planets['Saturn']['Period'])*r_Saturn
                print("fillcircle",r_Xsat,r_Ysat,3)
                print("text ", "\"Saturn\"",r_Xsat+planets['Saturn']['Radius']*scale,r_Ysat)
            if "Mimas" in planets:
                r_Mimas=planets['Mimas']['Orbital Radius']*scale;
                print("circle",r_Xsat,r_Ysat,r_Mimas)
                r_Xmim=r_Xsat+math.sin(t*2*math.pi/planets['Mimas']['Period'])*r_Mimas
                r_Ymim=r_Ysat+math.cos(t*2*math.pi/planets['Mimas']['Period'])*r_Mimas
                print("fillcircle",r_Xmim,r_Ymim,3)
                print("text ", "\"Mimas\"",r_Xmim+planets['Mimas']['Radius']*scale,r_Ymim)
            if "Enceladus" in planets:
                r_Enceladus=planets['Enceladus']['Orbital Radius']*scale;
                print("circle",r_Xsat,r_Ysat,r_Enceladus)
                r_Xenc=r_Xsat+math.sin(t*2*math.pi/planets['Enceladus']['Period'])*r_Enceladus
                r_Yenc=r_Ysat+math.cos(t*2*math.pi/planets['Enceladus']['Period'])*r_Enceladus
                print("fillcircle",r_Xenc,r_Yenc,3)
                print("text ", "\"Enceladus\"",r_Xenc+planets['Enceladus']['Radius']*scale,r_Yenc)
            if "Tethys" in planets:
                r_Tethys=planets['Tethys']['Orbital Radius']*scale;
                print("circle",r_Xsat,r_Ysat,r_Tethys)
                r_Xtet=r_Xsat+math.sin(t*2*math.pi/planets['Tethys']['Period'])*r_Tethys
                r_Ytet=r_Ysat+math.cos(t*2*math.pi/planets['Tethys']['Period'])*r_Tethys
                print("fillcircle",r_Xtet,r_Ytet,3)
                print("text ", "\"Tethys\"",r_Xtet+planets['Tethys']['Radius']*scale,r_Ytet)
            if "Dione" in planets:
                r_Dione=planets['Dione']['Orbital Radius']*scale;
                print("circle",r_Xsat,r_Ysat,r_Dione)
                r_Xdio=r_Xsat+math.sin(t*2*math.pi/planets['Dione']['Period'])*r_Dione
                r_Ydio=r_Ysat+math.cos(t*2*math.pi/planets['Dione']['Period'])*r_Dione
                print("fillcircle",r_Xdio,r_Ydio,3)
                print("text ", "\"Dione\"",r_Xdio+planets['Dione']['Radius']*scale,r_Ydio)
            if "Rhea" in planets:
                r_Rhea=planets['Rhea']['Orbital Radius']*scale;
                print("circle",r_Xsat,r_Ysat,r_Rhea)
                r_Xrhe=r_Xsat+math.sin(t*2*math.pi/planets['Rhea']['Period'])*r_Rhea
                r_Yrhe=r_Ysat+math.cos(t*2*math.pi/planets['Rhea']['Period'])*r_Rhea
                print("fillcircle",r_Xrhe,r_Yrhe,3)
                print("text ", "\"Rhea\"",r_Xrhe+planets['Rhea']['Radius']*scale,r_Yrhe)
            if "Titan" in planets:
                r_Titan=planets['Titan']['Orbital Radius']*scale;
                print("circle",r_Xsat,r_Ysat,r_Titan)
                r_Xtit=r_Xsat+math.sin(t*2*math.pi/planets['Titan']['Period'])*r_Titan
                r_Ytit=r_Ysat+math.cos(t*2*math.pi/planets['Titan']['Period'])*r_Titan
                print("fillcircle",r_Xtit,r_Ytit,3)
                print("text ", "\"Titan\"",r_Xtit+planets['Titan']['Radius']*scale,r_Ytit)
            if "Iapetus" in planets:
                r_Iapetus=planets['Iapetus']['Orbital Radius']*scale;
                print("circle",r_Xsat,r_Ysat,r_Iapetus)
                r_Xiap=r_Xsat+math.sin(t*2*math.pi/planets['Iapetus']['Period'])*r_Iapetus
                r_Yiap=r_Ysat+math.cos(t*2*math.pi/planets['Iapetus']['Period'])*r_Iapetus
                print("fillcircle",r_Xiap,r_Yiap,3)
                print("text ", "\"Iapetus\"",r_Xiap+planets['Iapetus']['Radius']*scale,r_Yiap)
            if "Uranus" in planets:
                r_Uranus=planets['Uranus']['Orbital Radius']*scale;
                print("circle",x,y,r_Uranus)
                r_Xura=x+math.sin(t*2*math.pi/planets['Uranus']['Period'])*r_Uranus
                r_Yura=y+math.cos(t*2*math.pi/planets['Uranus']['Period'])*r_Uranus
                print("fillcircle",r_Xura,r_Yura,3)
                print("text ", "\"Uranus\"",r_Xura+planets['Uranus']['Radius']*scale,r_Yura)
            if "Puck" in planets:
                r_Puck=planets['Puck']['Orbital Radius']*scale;
                print("circle",r_Xura,r_Yura,r_Puck)
                r_Xpuc=r_Xura+math.sin(t*2*math.pi/planets['Puck']['Period'])*r_Puck
                r_Ypuc=r_Yura+math.cos(t*2*math.pi/planets['Puck']['Period'])*r_Puck
                print("fillcircle",r_Xpuc,r_Ypuc,3)
                print("text ", "\"Puck\"",r_Xpuc+planets['Puck']['Radius']*scale,r_Ypuc)
            if "Miranda" in planets:
                r_Miranda=planets['Miranda']['Orbital Radius']*scale;
                print("circle",r_Xura,r_Yura,r_Miranda)
                r_Xmira=r_Xura+math.sin(t*2*math.pi/planets['Miranda']['Period'])*r_Miranda
                r_Ymira=r_Yura+math.cos(t*2*math.pi/planets['Miranda']['Period'])*r_Miranda
                print("fillcircle",r_Xmira,r_Ymira,3)
                print("text ", "\"Miranda\"",r_Xmira+planets['Miranda']['Radius']*scale,r_Ymira)
            if "Ariel" in planets:
                r_Ariel=planets['Ariel']['Orbital Radius']*scale;
                print("circle",r_Xura,r_Yura,r_Ariel)
                r_Xari=r_Xura+math.sin(t*2*math.pi/planets['Ariel']['Period'])*r_Ariel
                r_Yari=r_Yura+math.cos(t*2*math.pi/planets['Ariel']['Period'])*r_Ariel
                print("fillcircle",r_Xari,r_Yari,3)
                print("text ", "\"Ariel\"",r_Xari+planets['Ariel']['Radius']*scale,r_Yari)
            if "Umbriel" in planets:
                r_Umbriel=planets['Umbriel']['Orbital Radius']*scale;
                print("circle",r_Xura,r_Yura,r_Umbriel)
                r_Xumb=r_Xura+math.sin(t*2*math.pi/planets['Umbriel']['Period'])*r_Umbriel
                r_Yumb=r_Yura+math.cos(t*2*math.pi/planets['Umbriel']['Period'])*r_Umbriel
                print("fillcircle",r_Xumb,r_Yumb,3)
                print("text ", "\"Umbriel\"",r_Xumb+planets['Umbriel']['Radius']*scale,r_Yumb)
            if "Titania" in planets:
                r_Titania=planets['Titania']['Orbital Radius']*scale;
                print("circle",r_Xura,r_Yura,r_Titania)
                r_Xtita=r_Xura+math.sin(t*2*math.pi/planets['Titania']['Period'])*r_Titania
                r_Ytita=r_Yura+math.cos(t*2*math.pi/planets['Titania']['Period'])*r_Titania
                print("fillcircle",r_Xtita,r_Ytita,3)
                print("text ", "\"Titania\"",r_Xtita+planets['Titania']['Radius']*scale,r_Ytita)
            if "Oberon" in planets:
                r_Oberon=planets['Oberon']['Orbital Radius']*scale;
                print("circle",r_Xura,r_Yura,r_Oberon)
                r_Xober=r_Xura+math.sin(t*2*math.pi/planets['Oberon']['Period'])*r_Oberon
                r_Yober=r_Yura+math.cos(t*2*math.pi/planets['Oberon']['Period'])*r_Oberon
                print("fillcircle",r_Xober,r_Yober,3)
                print("text ", "\"Oberon\"",r_Xober+planets['Oberon']['Radius']*scale,r_Yober)
            if "Neptune" in planets:
                r_Neptune=planets['Neptune']['Orbital Radius']*scale;
                print("circle",x,y,r_Neptune)
                r_Xnep=x+math.sin(t*2*math.pi/planets['Neptune']['Period'])*r_Neptune
                r_Ynep=y+math.cos(t*2*math.pi/planets['Neptune']['Period'])*r_Neptune
                print("fillcircle",r_Xnep,r_Ynep,3)
                print("text ", "\"Neptune\"",r_Xnep+planets['Neptune']['Radius']*scale,r_Ynep)
            if "Titan" in planets:
                r_Titan=planets['Titan']['Orbital Radius']*scale;
                print("circle",r_Xnep,r_Ynep,r_Titan)
                r_Xtita=r_Xnep+math.sin(t*2*math.pi/planets['Titan']['Period'])*r_Titan
                r_Ytita=r_Ynep+math.cos(t*2*math.pi/planets['Titan']['Period'])*r_Titan
                print("fillcircle",r_Xtita,r_Ytita,3)
                print("text ", "\"Titan\"",r_Xtita+planets['Titan']['Radius']*scale,r_Ytita)

            t += 0.003


print(draw(x, y, t, planets))

© Stack Overflow or respective owner

Related posts about python

Related posts about function