6. Now you try! - Cubes by four isn't a list?



After typing in the code (below) I get an error reading "It looks like cubes_by_four isn't a list" but I can't figure out what's wrong with my code? All help previously posted on this forum is the same as what I typed in but mine won't work?

I thought the code was fine, It should return a list of cubed numbers (between 1 and 10) that are also divisible by four.

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


You were told to use list_comprehension, List comprehension does not require a function,

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

All you need to do is remove this line,


Good lord.. I feel silly. The defined function was there already so I just left it and didn't really take it into consideration! Thanks so much!


You're welcome :slight_smile:


I didn't get why you put the x**3 can you explain? I didn't really understand it


I didn't get that either :worried:


x**3 = x cubed. the question asks you to cube it.


I know that x**3 = x cubed
But why "x**3" why not for intence "x**2"

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


x**2 is x squared. You are not answering the question.


The challenged posed was:

1) Use a list comprehension to create a list, cubes_by_four.
2)The comprehension should consist of the cubes of the numbers 1 through 10 only if the cube is evenly divisible by four.
3)Finally, print that list to the console.
Note that in this case, the cubed number should be evenly divisible by 4, not the original number."

As you can see, the question asks for cubes of the numbers in the list to be printed if divisible by four. If the question asked fr squares of the numbers then x**2 would be fine.


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