pythonrhino3d

Else statement counter within function


import rhinoscriptsyntax as rs

def conCir(pt, r):
    if r <= 0:
        print "Done"
    else:
        rs.AddCircle(pt, r)
        return conCir(pt, r-1)

pt1 = rs.GetPoint("Pick First Point")
pt2 = rs.GetPoint("Pick Second Point")
r = rs.Distance(pt1, pt2)
conCir(pt1, r)

What I try is to make concentric circles, however, I can't figure out how to count the number of circles created. In an ideal situation, this counter is embedded within the function, but placing it on the first line makes it reset to 0 every time the function is called.

To be clear, when finished the function needs to print "Done", count, "circles were drawn."


Solution

  • Pass a counter in the recursive call, and print it when printing Done

    def conCir(pt, r, count=0):
        if r <= 0:
            print "Done"
            print count, " circles were drawn"
        else:
            rs.AddCircle(pt, r)
            return conCir(pt, r-1, count+1)