Reputation:
I couldn't find my mistake in this code below. If I say "return" instead of "print" in else part, code can not execute else part, it only makes calculations in if part. How can i fix this?
def calculatePerimeter(length, depth):
if depth == 1:
return 3 * length
else:
print (calculatePerimeter(length, depth-1) * (4/3)**(depth)) / ((4/3)**(depth-1))
calculatePerimeter(100, 3)
Upvotes: 2
Views: 7913
Reputation: 781004
You need to return the value in the else
clause, otherwise there's nothing to multiply (except when making the last call in the recursion). Then you need to call print
when calling the function.
def calculatePerimeter(length, depth):
if depth == 1:
return 3 * length
else:
return (calculatePerimeter(length, depth-1) * (4/3)**(depth)) / ((4/3)**(depth-1))
print(calculatePerimeter(100, 3))
Upvotes: 1