[英]TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
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.如果我在 else 部分说“return”而不是“print”,则代码不能执行 else 部分,它只在 if 部分进行计算。 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)
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).您需要在else
子句中返回值,否则没有任何东西可以相乘(除非在递归中进行最后一次调用)。 Then you need to call print
when calling the function.然后你需要在调用函数时调用print
。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.