I am struggling to add numbers by using python. Could you please aid me. Thank you.
Asked
Active
Viewed 1.2k times
-9
-
You can use the addition operator `+` or the [`sum`](https://docs.python.org/3/library/functions.html#sum) function – Patrick Haugh Feb 16 '17 at 16:43
-
i dont want to use the sum function. Also if i have like 300 numbers i can add them individually – SyntaxError101 Feb 16 '17 at 16:45
-
7What have you tried? Have you familiarized yourself with basic Python syntax yet? This is an extremely elementary question that I'm sure can be resolved with a little work to learn the bare minimum elements of the language. – Nicholas Flees Feb 16 '17 at 16:48
-
doing so as we speak – SyntaxError101 Feb 16 '17 at 16:50
1 Answers
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