[Tutor] for Loops with Squared
Mary Knauth
maryknauth at mac.com
Sat May 23 11:08:48 EDT 2020
Hello!
I am just diving into Python, and I’m having difficulty with for Loops right now.
The exercise I am working on is:
We will provide you with a value N. You should calculate the sum of each value of N squared from 0 up to and including N.
In mathematical notation, this is written as ∑Ni=0i2
∑
i
=
0
N
i
2
So if we passed in 3, you would output 02+12+22+32=14
My script is:
# Get N from the command line
import sys
N= int(sys.argv[1])
square = 0
for i in range(0, N+1):
square = (i * i) + square
print(square)
I’m pretty sure my mistake is in line 10, but I’m missing what I’m doing incorrectly.
Thanks!
Warm Regards,
Mary
<https://zephyrsolutions.us/>
More information about the Tutor
mailing list