在Python中定义函数

时间:2016-02-20 21:14:17

标签: python

我有一个像这样的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

目前我无法返回任何价值,请告诉我这是什么问题?

1 个答案:

答案 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时在缩进中犯了错误。如果情况并非如此,那就是你原来的缩进,那么你也需要纠正它。

相关问题