Functions 6.Practice Makes Perfect / HELP


#1


Help please.

I got this error: by_three(3) resulted in an error: global name 'cubed' is not defined

def cube(number):
    cubed = number ** 3
    return cubed

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


#2

Are you trying to use the cubed function in the by_three function?

If you call a function in Python you always have to follow up the function name with paranthesis.
E.g. :

my_function # not the right way to call a function
my_function() # the right way to call a function

Also the cube function expects a parameter.

my_function(number) # calling a function with the number variable as the parameter

If you are trying to access the cubed variable you defined in the cube function: That's not possible because variables that you define in functions are out of scope.

Edit: Just realized that the function is called cube not cubed, so better go with Candy's answer.


#3

Lets review what you got wrong here and we are going to make it simpler!:

-(Line 2) First of all, you don't need a variable just do: `

return number**3

-Lets look at that error you got:
--That is because you defined that variable INSIDE the function. The variable will only work in that function. You can delete that variable.

-(Line 7) Instead of putting cubed put cube(n), basically it just calls that function.

Here is how it should look like:

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

`

Anymore questions? Just message me!

~Candy :slight_smile:


#4

haha! Made that mistake once to many times too. :stuck_out_tongue_closed_eyes:


#5

This didn't help. My code is practically identical to yours. Idk what I'm doing wrong. This is my code:

def cube(n):
return n ** 3

def by_three(n):

if n % 3 == 0:
    return cube(n)
else:
    return False

cube(n)


#6

1) Did you do your indenting right?

2) Delete the cube(n) at the bottom


#7

candycornlover1 solved my problem, thank you.
So code that worked for me is
def cube(number):
return number**3

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

Just number as parametar instead of n


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.