Now You Try!(Error)


#1

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

print cubes_by_four

out put:

[4, 16, 36, 64, 100]

i get this:

Oops, try again. It looks like your list has the wrong value at index 0. It should be 8.

#2

Hi here

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

You want the cube of x so it should be like that (x**3) instead of x*x do the same in the if statement


#3

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

print cubes_by_four

Thank you :slightly_smiling:


#4

Even if the above worked shouldn't it be:
cubes_by_four = [x * x * x for x in range(1, 11) if (x*x*x)%4 == 0]


#5

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


#6

your are right! thank you!


#7

How come is this line justified? " if the cube is evenly divisible by four". I don't see this check. Is there something I'm missing?
Shouldn't it be something like this:

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


#8

Here's what I came up.
(This was tricky)

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

#9

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

output: [8, 64, 216, 512, 1000]
None

error message: Oops, try again.
You got the following error: global name 'cubes_by_four' is not defined. Did you create a list called cubes_by_four?

can somebody guide me as to where I am going wrong?


#10

Hi your list of comprehension should be cubes_by_four instead of cubes_of_four


#11

Yes, I realized that after I posted the question. Thanks anyways :slight_smile:


#12

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

print cubes_by_four


#13

Hi, what is the == 0 for at the end?


#14

Hi @xiaokiraa.
The ==0 at the end is used to check whether the mod 4 of the number gives remainder 0. Basically the mod function is used to divide a number and return the remainder after division.
So in the above code the cube of the number is divided by 4 and is checked whether the number after cubing is divisible by 4 using the ==0
Hope this helps :smile:


#15

why should it be x ** 3?


#16

Try this

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


#17

Try this one :relaxed:
cubes_by_four=[i**3 for i in range(1,11) if i%2==0]
print cubes_by_four


#18

I was just trying to figure that out too. I think it’s because the problem wants you to print x**3, so the x**3 at the start prints the cubed value and the x**3 in the if statement filters out the cubed numbers that are divisive by 4


#19

The instructions specify this …

The comprehension should consist of the cubes of the numbers 1 through 10 only if the cube is evenly divisible by four.

Based on that, it is the cube of each number that should be tested. Accordingly, the author of the course intended for the user to write this as the condition in the list comprehension …

n ** 3 % 4 == 0

There are various ways to accomplish the required outcome. Other conditions will also pass, provided that the result is that cubes_by_four includes all of the appropriate numbers, and only those numbers.


#20