[英]TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
[英]TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
我在下面的代码中找不到我的错误。 如果我在 else 部分说“return”而不是“print”,则代码不能执行 else 部分,它只在 if 部分进行计算。 我怎样才能解决这个问题?
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)
您需要在else
子句中返回值,否则没有任何东西可以相乘(除非在递归中进行最后一次调用)。 然后你需要在调用函数时调用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.