-9

I am struggling to add numbers by using python. Could you please aid me. Thank you.

SyntaxError101
  • 79
  • 1
  • 2
  • 11

1 Answers1

0

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.

Douglas
  • 1,304
  • 10
  • 26