函数内的 Else 语句计数器
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)
我尝试做的是制作同心圆,但是,我不知道如何计算创建的圆的数量。在理想情况下,这个计数器嵌入在函数中,但将它放在第一行会使它在每次调用函数时都重置为 0。
明确一点,函数完成后需要打印 "Done"、计数、"circles were drawn."
在递归调用中传递一个计数器,打印时打印出来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)
同Barmar的回答,扩展调试打印
Python 3 打印()
只是打印而不是调用 Circle 进行说明
为递归添加了默认变量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
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。
明确一点,函数完成后需要打印 "Done"、计数、"circles were drawn."
在递归调用中传递一个计数器,打印时打印出来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)
同Barmar的回答,扩展调试打印
Python 3 打印()
只是打印而不是调用 Circle 进行说明
为递归添加了默认变量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