[Tutor] for Loops with Squared

alexkleider alexkleider at protonmail.com
Sat May 23 14:45:21 EDT 2020


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Saturday, May 23, 2020 8:08 AM, Mary Knauth via Tutor <tutor at python.org> wrote:

> 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.
>
> 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.
>

Although not tested your code seems correct to me.
If you pass in 3, the calculation (it seems to me) should be
1*1+2*2+3*3 => 14



More information about the Tutor mailing list