Now you try


#1

Hi I am stuck please help!!
This is my code
cubes_by_four = [‘8’ for x in range(1,11) if x % 4]
print cubes_by_four

This is what it said is wrong

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


#2

8 is the cube of 2. Use the variable…

[x ** 3 for x ...]

This will be True for all values not divisible by 4. What does the instruction ask for?

Please post a link to the exercise when creating a topic. Thanks.


#3

it asks for this

Use a list comprehension to create a list, cubes_by_four.

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

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.

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

The error
File “python”, line 2
print cubes_by_four
^
SyntaxError: invalid syntax


#4

The syntax error is the extra leading bracket.

Based upon the question then we need to test if the cube is divisible by 4.

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

Note the slight difference in the conditional. We want the cube to be divisible by 4, not the actual numbers 1 to 10.

2 ** 3 == 8      # check
4 ** 3 == 64     # check
6 ** 3 == 216    # check
8 ** 3 == 512    # check
10 ** 3 == 1000  # check

The following statement is True when the number is NOT divisible by 4:

y % 4

The following is True when the number IS divisible by 4

y % 4 == 0

y in this exercise is x ** 3.

From this experiment we may conclude that all even numbers when cubed are divisible by 4.

Making sense?


#5

thank you it worked!!:grinning:


#6

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