Intro to Python Scripting: 13 Simple Recursion
Course or Collection:
Tag:
Video Duration:
13 minutes
In this tutorial we take a look at Recursion through a couple of simple examples. Recursion can be a bit confusing at times, so have a cup of coffee ready for this one. Basically, recursive functions are functions that call themselves. Generally, they keep calling themselves until some condition is met, at which point they exit. This is critical, because you can very easily create an infinite loop, which will crash Rhino. Trust me I did it a couple of times while writing the next tutorial. :)
#Factorial def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print factorial(5)
#Fibonacci Sequence #1,1,2,3,5,8,13,21 def fib(n): if n == 0: return 0 if n == 1: return 1 return fib(n-1) + fib(n-2) for i in range(0,15): print fib(i)
import rhinoscriptsyntax as rs def RecursiveCircle(pt, r): if r == 0: return 1 else: rs.AddCircle(pt, r) return RecursiveCircle(pt, r-1) pt = rs.GetPoint("Pick starting point") RecursiveCircle(pt, 10)
Want to Contribute?
Want to be an author? Drop us a line here we'd love to have you.
Already have a video you'd like to post? Send us a link and we'll get you going.
:)
Comments
reyob replied on Permalink