Now You Try!


#1



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.


#2

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

currently you check if x^3 is divisible by 4


#3

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


#4

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


#5

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?


#6

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

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


#7

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

#8

here:

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


#9

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...


#10

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?


#11

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?


#12

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.


#13

here:

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


#14

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


#15

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


#16

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