Practice Makes Perfect help


#1

So the code seems to be working but it keeps throwing me an incorrect saying "Oops, try again. by_three(2) returned True instead of False "

Which doesn't make sense because I WANT it to return true otherwise that means the math doesn't work based on whatever (number) is. Also I don't know why it's saying by_three(2) when I didn't define (number) anywhere in the code at all. It seems to be pulling that 2 out of it's arse.

What gives?


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


#2

Here you are telling python that if "cube(number)/3" got a value it will return True (So it will always return True if you give any number to that function), probably you can fix that by editing that condicion.


#3

Ah, I thought return bools were conditional inherently. If not true or false, then what should I return to output a legit answer? Do I need to create parameters for (number) at the bottom like cube(12) and then have it return itself?

EDIT: tried a different solution that should work on paper according to the instructions, but I'm getting a maximum recursion depth error.


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

#4

I don't think this it's the answer, what does the instructions tells you to do?


#5

1.First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon!
2.Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).
3.Define a second function called by_three that takes an argument called number.
4.if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.

Everything in my current codeblock falls directly into what the instructions say, so I have no idea what they expect here.


#6

To check if a number it's divisible by 'x' you need to use the modulus operator '%' and perform the operation "number % x" if the result it's 0 them it's divisible by 'x'.
Something like this:

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

Hope that solves your question.


#7

Do I need to use modulo for division? This page: http://www.tutorialspoint.com/python/python_basic_operators.htm says division is just / if I don't want to get the remainder, and if something is divisible by three perfectly there should be no remainder. What divided by 3 would equal zero anyway? Anything multiplied by zero would just be zero, and you can't divide by zero so that makes no sense.


#8

Not really, let's show you with an example: case 1: 9/3 = 3, case 2: 9%3 = 0.
In the first case you just got the answer and in the second one you got the remainder, and the remainder will always be 0 if the number it's divisible by 3.

You can add parentheses if that makes it clearer for you.


#9

Oh, 0 IS the remainder, I see now. It seems like it should work both ways though, either the answer is a whole number or the remainder is zero, therefore the variable is a whole number anyway. Is there an advantage of using one over the other?


#10

What do you mean?

To clarify:
The function will return the cube of the number if the remainder is 0, otherwise it will return 'False'.


#11

Exactly. If I do x%3==0, then the number is divisible by three because it has no remainder. If I just do x/3 whatever number replaces x has to be divisible by three or else the program will reject it and report false. Both ways the program should work.


#12

Exactly, it just performs the operations x%3 and if that result it's 0, them the number it's divisible by 3, because it has no remainder

If you put x/3 in an If statement, it will always be True if the result it's diferent to 0, because it will perform the operation x/3 and if the result it's a number greater than 0 or less than 0 then it will go in, so it will not work as expected.

I suggest you to take a read of this.


#13

Thanks mate, every little bit helps, I appreciate this.


#14

Not to butt in, just dropping by with a little marginal thinking...

>>> x = float(13) / 7
>>> y = x - int(x)
>>> z = int(x - y)
>>> r = int(round(y * 7))
>>> x, y, z, r
(1.8571428571428572, 0.8571428571428572, 1, 6)
>>>

The Dividend, 13 over the divisor, 7 gives quotient, 1 and Remainder, 6.

7 * 1 + 6 == 13

d * q + R == D

which when given in a generalized math expression might look like,

d * q = D - R

or

R = D - d * q

#15

I have written similar code but I'm getting indentation error..can anybody help?


#16

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