6. Practice makes perfect, answer?


#1

Need help on this lesson


I don't know what's wrong
#2

What do you want to know exactly?


#3

this is my code , i don't know how to multiply three times , i tried * & ** but got refused
def cube(n):
return n**3
def by_three(n):
if n%3 == 0 :
return cube(n)
else :
return False
n = raw_input


#4

finally i've solved it
[Code removed by moderator]


#5

please help me on 5/19 Functions Calling Functions please help


#6

spacing
[def cube(n):
tab cubed = n**3
tab return cubed
def by_three(n):
tab if n%3 == 0 :
tab tab return cube(n)
tab else :
tab tab return False
n = raw_input


#7

1st you put the error then your code and wait


#8

i am not sure this is correct chipjumper
return False
n = raw_input

because you need to define the value for function cube

so i believe should be something similar to
cube(10) cube(9) or what ever number you like within ()
but i might be wrong as i am not experienced


#10

I need help. This is my code:
def cube(n):
cubed=n**3
return cubed
def by_three(n):
if n%3 == 0:
print "n is divisible by 3"
return cube(n)
else :
print "n is not"
return False
n=raw_input

I get a error message saying that the "d" in cubed on the second line is an expected an indented block.


#11

I think your indention is off, should be more like

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

def by_three(n):
    if n%3 == 0:
        print "n is divisible by 3"
        return cube(n)
    else :
        print "n is not"
        return False

n=raw_input # Why is this in your program?

#12

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

That's what I have and it passed. Although I notice I returned and modified the argument all in one step. Is that still correct (againit passes) and is it 'better' to cube number then call the function 'cube' on a new line?


#13

Hey,

i was just wondering why do you have to put the == 0

thanks


#14

Thx, it worked! I dont really know what the "n=raw_input" was. I just saw that someone else had that in their code and it worked for them n i thought i should give it a try :confused:


#15

I guess this was highly confusing for most of us :

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

def by_three(number):
    if  number % 3 == 0:
        return cube(number)
       
    else:
        return False
  • cube is not a returned statement, but rather cubed (It is assigned first though).

  • The second function is defined by by_three , whilst the argument is "still" number.

  • The rest is history rather
    :+1:

`


#16

why did you guys input "cubed" for your return?


#17

The argument number to the power of 3 ( number ^3 ) or in python terms number**3 is a cubed "cube" .
Logically it represents the argument number cubed
And to answer your question diligently we return whatever is defined within the function which in this case is the variable cubed.

More help How is returning the output of a function different than printing it?


#18

Need help on this lesson


#19

You have to do this because the "%" is basically a dividation symbol but instead of the answer being how many times the number goes into a number then whats left is the answer, so 5%3==2 and the == symbol is used for the code to check if both sides match up, so 3%3==0 3 goes into three with no remainder so its zero


#20

This is what I really don't get: if the value of the variable number is not specified anywhere in the code then how can it return anything? Yet the code worked. :confused:


#21

that 'if' statement is checking to see if the number is divisible by 3 and no remainder, it uses the modulus (%) to do this and since you are not assigning a value but are testing to see if it's true, you use '=='