Stuck at "Practice Makes Perfect"


#1



Practice Makes Perfect


I get an error message as soon as I launch it

My Error Message is:
Oops, try again. cube(1) resulted in an error: 'function' object has no attribute 'number'


I want my code to define cube then do a check if it's equal to a number, if it is I want the code to cube my variable cube(number), I did not put a else statement because the instructions didnt say to

I also expect my code to create a new function called by_three which I assigned the argument number and if it's divisible by 3 return cube and otherwise return False (also, can someone explain why it returns False, i don't get that)


def cube(number):
    if cube == cube.number():
        return cube*cube*cube
        def by_three(number):
            if by_three / 3:
                return cube
            else:
                return False


#2

Hm....You can't just define a function in a function. What you can do is create another function outside (known as a helper function) and use it in another function by calling it.

Therefore, define your by_three function outside. Now, there are several other things I would like to point out:

  1. In your cube function, you do not need to check if...number is...a number (if that is what you're checking). Just simply return.

  2. in that same function, it's better to write number**3 instead of cube*cube*cube. Also, cube is the function name and not the input variable. number is the variable you use.

  3. in your by_three function, remember by_three is the function name and not your input variable. should change the by_three in the if statement to number.

  4. Your if statement.....it's unfinished. If by_three / 3 is what? equal to one? two? If you want to say that the number is divisible by 3, use if number % 3 == 0 as % gives you the remainder when you divide number by 3.

  5. Your return cube is incomplete. You didn't call the cube function properly. You must call it with it's input variable like this: cube(number).

The reason why your function returns False is because your if statement was incomplete hence the function just ignores it and moves on to the else statement.


#3

And by moving your by_three function outside, i mean this:

def cube(number):
    if cube == cube.number():
        return cube*cube*cube

def by_three(number):
    if by_three / 3:
        return cube
    else:
         return False

#4

Hello, I am confussed about something. If you define a function named cube and assign it the argument number what does cube equal? I know it equals a number, but how does it get assigned this number that I don't know?

Thank you.


#5

Cube is simply a function name. (Number) beside the cube is the input and the whole function produces an output. It doesnt have to be a number but in this case, cube returns number^3. Without the (number) you wont be able to input something and thus cant calculate.
Cube=number^3 is basically what youre trying to write in Python. Number can be any number.


#6

How do you input the argument number? For example; on a calculator, I click 3 then ^ then 3, on python how am I inputting?

Thank you for the help. Much appreciated.


#7

An expression like this: cube = x^3 where x is a variable and can be any number-- That's what youre trying to code in Python. It isnt just 3^3. You already have your input (number) like so:

def cube(number)
    return number**3

And you can put in any number by calling the function like so:
eg.
cube(2)
And 8 will pop up in the terminal.


#8

Thank you for your help. I completed it successfully with your help.


#9

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