我有一个像这样的Python代码,
def cube(n):
n = n * n * n
return n
def by_three(n):
if (n % 3 == 0):
print "number is divisible by three"
cube(n)
return n
else:
print "number is not divisible by three"
return False
目前我无法返回任何价值,请告诉我这是什么问题?
答案 0 :(得分:6)
您未在cube(n)
函数中设置by_three
的值以便返回。
def cube(n):
result = n ** 3 # You can use ** for powers.
return result
def by_three(n):
if (n % 3 == 0):
print "number is divisible by three"
result = cube(n)
return result
else:
print "number is not divisible by three"
return False
我还假设你在复制到SO时在缩进中犯了错误。如果情况并非如此,那就是你原来的缩进,那么你也需要纠正它。