Why does my cubes_by_four list not contain the right values?

Question

Why does my cubes_by_four list not contain the right values?

Answer

Some of the most common issues are:

  1. Using the wrong range: recall that range() includes the first number and excludes the stopping number, so if we want to go from 1 to 25, we’d write range(1, 26)
  2. Incorrectly checking if the cube is divisible by four: to check if something is evenly divisible by a number, we write if x % number == 0
  3. Not cubing the number: to cube something, write x ** 3
3 Likes

I’m not sure if I’m doing this right, but my code adheres to what is written above and looks like this…

cubes_by_four = [x ** 3 for x in range(1, 11) if x % 4 == 0]

print cubes_by_four

However, it only prints the values [64, 512] rather than including the other three values that fit the code. What am I doing wrong?

6 Likes

Solved it. I didn’t pay attention to the reminder saying that the cubed number must be divisible by 4, not the original number. My final code looks like this…

cubes_by_four = [x ** 3 for x in range(1, 11) if x ** 3 % 4 == 0]

print cubes_by_four

6 Likes

wasn’t clear to me what cubed numbers meant. Thanks for the clarification.

2 Likes

Hey, I came across exactly the same exercise on CodeAcademy Python2 free online course! Congratulations…

1 Like

I don’t understand what it meant by “Note that in this case, the cubed number should be evenly divisible by 4, not the original number.”
What’re the original numbers supposed to be?
Like, what’s the difference between:
[x ** 3 for x in range(1, 11) if (x ** 3) % 4 == 0]
and
[x ** 3 for x in range(1, 11) if x % 4 == 0]

The last line of the challenge actually muddies things up a bit. Perhaps it should be omitted, or re-worded like this:

Note that in this case, it is the cubed number that should be evenly divisible by 4; it does not matter if the original number is divisible by 4.

Your first example works; the second one does not.

It make sense. Thanks

Hi
Can someone explain me what is the problem here?

> cubes_by_four [x**3 for x in range (11) if x**3 % 4 == 0 ]

> print cubes_by_four

It looks like you’re missing the assignment operator, =, in the first line. Remember that we have to use this operator when assigning a a value to a variable.

I’m still having trouble understanding the purpose of the first section (c ** 3) in the list comprehension

cubes_by_four = [c ** 3 for c in range(1, 11) if c ** 3 % 4 == 0]

print cubes_by_four

Welcome to the forums!

The ** operator raises the operand on the left to the power of the operand on the right. So c ** 3 is an expression that equates to "c to the power of 3", which is c cubed. Since we want the cubes that are divisible by 4, we need to cube each number in range(1, 11) and check if it’s divisible by 4 before putting it in the list.

The question asks for the cubed numbers to be divisible by 4 hence we follow the same logic.
cubes_by_four =[x3 for x in range(1,11)if x3%4==0]

print cubes_by_four