Here is how you would do it in Python 2:
n = int(raw_input("Type n:"))
total = 0
for i in range(1,n+1):
total += i**2
print total
Here is how you would do it in Python 3:
n = int(input("Type n:"))
total = 0
for i in range(1,n+1):
total += i**2
print(total)
This takes the numbers 1 through n
and put them into variable i
. For each time this happens, it takes the square of i
and adds it onto total
. It then prints the total.
I suggest reading a Python book. “Hello World” by Carter and Warren Sande is a fantastic one!
If you need further clarification, notify me in the comments.
solved Python code to sum the square values of numbers from 1 to n