(初学者)Python函数Codecademy

时间:2013-08-02 15:39:07

标签: python

我正在学习Codeacademy的编程。我有一个任务,但无法弄清楚我做错了什么。

首先,我需要定义一个返回值的多维数据集的函数。然后我应该定义第二个函数,检查一个数字是否可以被3整除。如果是,我需要返回它,否则我需要返回False

继承人代码:

def cube(c):
    return c**3

def by_three(b):
    if b % 3 == 0:
         cube(b)
         return b
    else:
         return False

6 个答案:

答案 0 :(得分:8)

您没有捕获函数cube的返回值。做b = cube(b)。或者更好的是,return cube(b)

def cube(c):
    return c**3

def by_three(b):
    if b % 3 == 0:
         b = cube(b)
         return b  # Or simply return cube(b) and remove `b = cube(b)`
    else:
         return False

当您使用参数cube调用b函数时,它返回传递参数的多维数据集,您需要将其存储在变量中并将其返回给用户,在当前代码中,你忽略了返回的价值。

答案 1 :(得分:2)

我认为这个答案也可能有用:

    def cube(b,c):
        b = c ** 3
        if b % 3 == 0:
            return b
        else:
            return False
        return b

我知道这可能有点多余,但我认为这可能是做你正在做的事情的另一种方式。我认为Sukrit更简单。

答案 2 :(得分:0)

我已经完成了Codecdemy,这是我的代码。

def cube(n):
    return n ** 3
def by_three(number):
    if number % 3 == 0:
       return cube(number)
else:
    return False

答案 3 :(得分:0)

这是我刚刚开发的轻量级解决方案,应该可以工作:)

def cube(number):
    return number**3

def by_three(number):
    if number % 3 == 0:
        return cube(number)
    else:
        return False

答案 4 :(得分:0)

def cube(num):
    return n ** 3

def by_three(value):
    if value % 3 == 0:
        return cube(value)
    else:
        return False

答案 5 :(得分:0)

尝试为每个人拨打同一封信。而不是使用' c'和' b',只需使用' c'

def cube(c):
    return c**3
def by_three(c):
    if c % 3 ==0:
        return cube(c)
    else:
        return False 
相关问题