简体   繁体   中英

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

Is everything right with code_cademy 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 ?

在此处输入图像描述

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]

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

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.

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.

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)

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 ! :)

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.

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)

帮助我的事情是意识到范围不包括1,即: range(0,9)只会做0-8,除非你想要0-8,否则正确的做法是0-10。

Taken 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM