2 posts were split to a new topic: Practice makes perfect - cube

Another broken lesson, sigh

Broken how? Can i see your code and error message? Then i can make a bug report.

If you’re getting the “Failed to test your code” error, try refreshing the page. It fixed the problem for me after going through several frustrating lessons just dealing with it.

why its not printing the result?

Please see this topic:

How to ask good questions (and get good answers)

without seeing your code and other relevant information, its really difficult to tell what the problem might be. The most obvious cases being no print statement/function call or the print statement isn’t executed/reached for some reason.

Hi, i’m a beginner and i’m lost on this lesson. Any help/advice?

this is what i wrote:

def cube(number):

return number*3

def by_three(number)

if number % 3 == 0:

return cube(number)

else:

return print “enter different number”

There is some math in the lesson:

- exponentiation
- modulo

The logic includes,

- functions
- conditional

The function our main call will go to (`by_three()`

) will conditionally call the helper function (`cube()`

).

First the maths…

```
a => symbol for any number
a ** 2 => exponential expression => a_squared
a ** 3 => a_cubed
```

We know that the square of a number is that number times itself…

```
a ** 2 => a * a
```

and the cube of a number is that number times itself, and times itself again.

```
a ** 3 => a * a * a
```

Generally, `a`

raised to any exponent `n`

is,

```
a ** n => a * a * ... * a (n times)
```

Module division yields a remainder from normal division instead of a quotient.

```
17 / 3 => 5, with 2 remainder
```

so,

```
17 % 3 => 2
```

Our helper function is very straight forward and it returns what the name says… *cube of number*

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

The main function takes a number and examines if it is divisible by 3 (n % 3 == 0) whereupon it returns the cube of that number.

```
if number % 3:
return cube(number) # call to the helper function
```

The return value from the main function will be the number returned from the helper function.

This line will return `None`

to our caller (the call to the main function) since `print()`

has no return. A better approach might be to simply return the message string, and print at the caller.

```
print (by_three(18)) # 5832
print (by_three(17)) # enter different number
```

Why does " number % 3" set to equal 0?

For example: “if number % 3 == 0:”

In the expression we use `==`

which is not an assignment. We are not setting, only comparing it to zero. The modulo operator is also called the **remainder operator** since it returns the remainder from division, not the quotient. Any number that is divisible by 3 will result in a zero remainder.

`if n % 3 == 0 => then `n` is evenly divisible by 3.`

Hi i used the print at the end of the code, but the values were not displayed.

Here is my code, can someone please explain me why this is not happening?

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

you nested the calls within the body of the function. Which would lead to recursion if you did call the function from outside the function/within the script

Hi guys,

Not sure if I’m being completely stupid but I’ve been stuck on this for a while now, and I can’t seem to find a solution in any of the provided resources.

def cube(number):

return number * number * number

def by_three(number):

if number % 3 == 0:

return cube(number)

else:

return “enter a valid number”

by_three()

by_three(3)

The code has changed a lot, I’ve tried using raw_inputs and moving everything around, but I’m completely at a loss.

Any help would be appreciated.

We cannot see your indentation. Hopefully the two call lines are not indented. If you wish to see the outcome, `print()`

your call expression.

Thanks for your response, sorry I forgot indentation was important but I think I’ve got that correct (hopefully).

I added print by_three, which got the error shown in the photo.

I’m really not sure what to do here, just want to pass this lesson and move on.

************ Edit ***********

I didn’t realise there was a check solution button, but doing so highlighted the problem.

It wasn’t a bollean false (if that makes sense).

Thanks for your help regardless!

Hi,

Regarding this exercise (Practice Makes Perfect), I can’t find out why *print* and *return* are different?

My code (apologies, for some reason my indents aren’t showing):

def cube(number):

return number * number * number

def by_three(number):

if number % 3 == 0:

return cube(number)

else:

return False

print cube(4)

print by_three(3)

print by_three(4)

This all prints:

64

27

False

So I understand this code! (Phew)

But the hint for this exercise says:

“Make sure both functions `return`

their values rather than `print`

ing them.”

Why, within the functions themselves, can I not replace *return* with *print*?

For example:

def cube(number):

print number * number * number

def by_three(number):

if number % 3 == 0:

print cube(number)

else:

print False

cube(4)

by_three(3)

by_three(4)

This prints:

64

27

None

False

So my question!:

Why is *print*, instead of *return* causing a problem here? Why has “None” appeared? What is the difference between *print* and *return* in this context?

Thank you for your help!

Kind regards,

Alex

(2nd time through the Codecademy Python 2 course by the way and enjoying it a lot more this time! Assuming I’m not alone in nothing making any sense first time round!?)

print and return do different things. print just prints some output. return hands back data to the caller.

if a function doesn’t return anything, you get None (which means absence of a return value)

Thank you for your reply!

That’s some good clarification. What I still don’t understand though is, why does *print* not work within the function in this code? Why must I choose *return* instead? and refrain from using *print* until I call the function’s argument?

To my (clearly misunderstood) logic, either way of writing the code should create the same result in the terminal…

Thanks!

exercise validation. Normally this would depend on program requirements, but return would have my preference, given then we can use the result for other things.

Nice. OK thank you for this clarification! Good to know why return is often preferred. Thank you!