Now You Try!


Could someone explain the process of getting those five values being divisible four, linked with the ranges 1,11 please.

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

Replace this line with your code.


if x is divisible by 4, append x^3 to the list

currently you check if x^3 is divisible by 4


Sorry for the late reply. Why does it give only five values when range is "1,11"?


Did you fix your if condition? values will only be inserted into list when if condition is true


It gives me five values as stated in the exercise, isn't the if statement correct if it prints out the values divisible by 4?


if x is divisible by 4, not x^3 divisible by 4

if you need more help, post an updated version of your code


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



if (x**3) % 4 == 0

you check if x to the power of 3 is divisible by 4 while you should check if x is divisible by 4, like i told you 5 times by now and as it states in the instructions


Ok Stetim I know your helping but I didn't know I need help with my code. Just needed clarification on what specifically is going on within the code.
I mean, it passes the exercise (the code). If it passes and I'm still doing something wrong then how am I supposed to be learning sufficiently...


Ok so:

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

x^3 is replaced with x as you had said. What is the next step?


Sorry. My mistake

No, you are doing it right. I got it wrong, sorry for that

What are you struggling to understand? The whole concept of list comprehension?


I didn't even know that I was right, which is probably even worse.

The thing that I'm trying to understand:
x to the power of 3 to the ranges from 1 to 11, gives 5 values,

(I'm trying to think of a question that will give us a more than one answer).

Let's start with why it only gives us five values.



cubes_by_four = [x**3

x**3 is what is appended to the list. But only when the if condition is true:

if (x**3) % 4 == 0

so only if x^3 is true x^3 will be appended to the list. so for example 1^3 = 1, 1 is not divisible by 4, so its not appended to the list


Why doesn't it print 2,000? Why only 5 values...


i ran your code here, it doesn't output 2000, it output the list it should


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