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


#1



https://www.codecademy.com/en/courses/python-beginner-en-KAgt5/1/3?curriculum_id=4f89dab3d788890003000096


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


#2

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,


#3

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!


#4

You're welcome :slight_smile:


#5

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


#6

I didn't get that either :worried:


#7

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


#8

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]


#9

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


#10

The challenged posed was:

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


#11

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