其他声明计数器在功能范围内

时间:2017-02-01 22:49:06

标签: python rhino3d

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)

我尝试制作同心圆,但是,我无法弄清楚如何计算创建的圆圈数。在理想情况下,此计数器嵌入在函数中,但将其置于第一行使得每次调用函数时它都会重置为0.

要说清楚,完成后功能需要打印“完成”,计数,“画圆圈。”

2 个答案:

答案 0 :(得分:1)

在递归调用中传递一个计数器,并在打印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)

答案 1 :(得分:0)

与Barmar的答案相同,扩展了调试打印

Python 3 print()

只是打印而不是调用Circle来说明

为递归添加了默认的var count = 0

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

conCir('fred', 5) # count default = 0, no need to enter in call

AddinCircle  fred 5
AddinCircle  fred 4
AddinCircle  fred 3
AddinCircle  fred 2
AddinCircle  fred 1
Done count=  5