newguy newguy - 1 year ago 123
Python Question

Print cubes of the numbers 1 through 10 only if the cube is evenly divisible by four

Is everything right with code_cademy here ?

enter image description here

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

for x in cubes_by_four:
print x

They are asking me to print each cube which is evenly divisible by four for numbers between 1 to 10. What am I doing wrong here ?

Also is this notation
allowed to get the cube of x ? if yes then why does is it results in wrong output ?

enter image description here

Answer Source

Finally print that list to the console

>>> cubes_by_four = [x**3 for x in range(1,11) if x**3 % 4 == 0]
>>> print(cubes_by_four)
[8, 64, 216, 512, 1000]

It says print the list, not print each item in the list to console

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download