Why does my cubes_by_four list not contain the right values?
Answer
Some of the most common issues are:
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)
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
Not cubing the number: to cube something, write x ** 3
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]
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]
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.
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.