Python 代码求和从 1 到 n 的数字的平方值

Python code to sum the square values of numbers from 1 to n

我正在努力使用 python 添加数字。你能帮帮我吗?谢谢。

这是 Python 2 中的操作方法:

n = int(raw_input("Type n:"))
total = 0
for i in range(1,n+1):
    total += i**2
print total

这是 Python 3 中的操作方法:

n = int(input("Type n:"))
total = 0
for i in range(1,n+1):
    total += i**2
print(total)

这会将数字 1 到 n 放入变量 i 中。每次发生这种情况时,它都会取 i 的平方并将其加到 total 上。然后打印总数。

我建议阅读 Python 一本书。 "Hello World" Carter 和 Warren Sande 的作品非常棒!

如果您需要进一步说明,请在评论中通知我。