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

5 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