print sum(x) - python

noo*_*oob -1 python sum python-3.x

有人可以帮我这个.我总是得到这个代码的错误,但不知道什么是错的?

x= 1523

while x <= 10503:
    x=x+2
    print sum(x)
Run Code Online (Sandbox Code Playgroud)

编辑.抱歉不清楚.是的,我想得到从1523到10503(包括)的所有数字的总和(间隔为2).

Woo*_*ble 6

pythonic解决方案是:

print(sum(range(1523, 10504, 2)))
Run Code Online (Sandbox Code Playgroud)

(使用while循环来构建包含一系列整数的列表,如果有一个内置函数可以更有效地执行它,那就有点傻了.)


Tim*_*ker 5

为什么要使用循环?

>>> ((10503-1523+2)//2  * (1523 + 10503))//2
27004383
Run Code Online (Sandbox Code Playgroud)

这只是旧问题的扩展"1到100之间所有整数的总和是多少?" 是的

50*101 (1+100 + 2+99 + 3+98 + ... 50+51)
Run Code Online (Sandbox Code Playgroud)