Python - Function Practice - What's wrong with this code? [solved]

def cube(number):
  return number * number * number

def by_three(number):
  number=int(raw_input ("enter a number that is divisble by three"))
  if number % 3 == 0:
    return cube(number)
  else:
    print "enter a valid number"
  by_three()

how do i make sure this loops and asks to enter valid number the get the cube

So currently, you ask for the function to be supplied with an argument of a number, but then promptly overwrite it with the raw_input request. If you ran the function within the same indentation as the else statement then it would run again if the else statement gets run. Just be careful of entering an infinite loop.

some on please help. the <if number % 3 ==0:> is killing me. 3 is not = to 0. so why is it 3 == 0?

It’s a comparison. We are checking if the result of an operation is equal to zero.

number % 3

is an expression that describes an operation that divides number by 3 and returns the remainder. So if we said,

remainder = number % 3

we could now write the comparison as,

if remainder == 0:

ok so what if it does not equal 0 or does

If x % n does not equal zero, then x is not divisible by n.

ok so i tested it. an ( if number % 3 == 0:) and if i put number(1) % 3 it still print the number or ever number(0) % 3 == 0 it still prints. how no matter if it equal 0 or not it still returns the number. how

Zero divided by anything is zero. Zero times anything is zero. Zero minus zero is zero. Put in the time to search for remainder operator to fill the gaps. It’s easiest to illustrate with pencil and paper using long division of integers.

ok, my last shot at it. this is how I’m looking at it. (if number % 3 == 0: ) let’s say the number 5 the remainder is going to come back 2. now that does not equal 0 but it still returns as if it does. now tell me where I went wrong and write how it is supposed to look Plz.

The rules for zero only apply to zero. 5 % 3 is 2 because when we remove the part that divides correctly, the remainder is 2. Pull out a pencil and some paper and long divide without decimals. Integer division, as it were.

d    D     q
7 ) 27  |  3
   -21         q * d
  ----
     6         D - q * d  =>  R

Quotient 3 Remainder 6, thus 27 % 7 is 6.


Legend

d  =>  divisor
D  =>  Dividend
q  =>  quotient
R  =>  Remainder