(初学者)Python函数Codeacademy

LtC*_*LtC 4 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
Run Code Online (Sandbox Code Playgroud)

Suk*_*lra 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
Run Code Online (Sandbox Code Playgroud)

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