'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 x^3 allowed to get the cube of x ? if yes then why does is it results in wrong output ?

enter image description here



Solution 1:[1]

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

Solution 2:[2]

When you write range(1,10), you include 1 but exclude 10.

So correct code is:

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

print cubes_by_four:

It will be a good practice to use x**3 for cubes.

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

Solution 3:[3]

Your range is off. You need range(1, 11), because the second argument of range() is the first value to exclude. range(1, 10) only gives you number 1 through 9.

Solution 4:[4]

If you want to include the value 10 you must change the range to range(1, 11), since the range does not include the second parameter.

In regular Python, x^3 does not mean exponentiation but rather the binary operation "bitwise exclusive or". That is exponentiation in SageMath (which is based on Python) but not in regular Python, which uses x**3, or as in your code, x*x*x.

Since you want to print out the list all in one line, including the surrounding brackets, just print using print x. Use that instead of your last two lines of code.

Solution 5:[5]

The second argument to range is not included in the range.

Is it between 1 and 10? or is it 1 through 10?

between 1 and 10 is range(2,10)

1 through 10 is range(1,11)

Solution 6:[6]

Okay , a couple of things to keep in mind :- 1) if you want numbers 1-10 , do range(1,11) , since the last number is excluded, while the first number is (obviously) , included. 2) instead of (x*x*x) , you can something better like :- pow(x,3) , which essentially means x to the power 3,or x cubed.

So,your final code becomes :- cubes = [pow(x,3) for x in range(1,11) if pow(x,3) % 4 == 0]

I hope this helps you , keep learning , getting stuck is a part of the wonderful journey in the world of programming . Cheers ! :)

Solution 7:[7]

There are two separate problems with the code you showed here:

First, change range(1,10) to range(1,11) because Python doesn't include the second parameter (10), and 10^3 is evenly divided by 4 (1000/4 = 250).

And finally, the tutorial wants you to print the numbers all in a single line, so just use print cubes_by_four instead of the for loop that you used to print each number in a different line.

Solution 8:[8]

script that prints the first 10 cube numbers (x**3), starting with x=1 and ending with x=10

for x in range(1,11):
 print(x*x*x)

Solution 9:[9]

The thing that helped me, was realizing that the range excludes 1, i.e.: range(0,9) will only do 0-8, the right way to do it unless you want 0-8 is 0-10.

Solution 10:[10]

From Codecademy

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.

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

Result:[8, 64, 216, 512, 1000] ie: Range of 5 cubed numbers (evenly divisible by 4)

Solution 11:[11]

for x in range(1,11):
    print(x*x*x)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Moses Koledoye
Solution 2
Solution 3
Solution 4
Solution 5 beauxq
Solution 6 Tushar C.
Solution 7
Solution 8 Ardent Coder
Solution 9 J. M. Arnold
Solution 10 Atomica
Solution 11 Roberto Caboni