[Tutor] for Loops with Squared

Richard Damon Richard at Damon-Family.org
Sat May 23 13:50:14 EDT 2020


On 5/23/20 11:08 AM, Mary Knauth via Tutor 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.
>
> 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
>
First question, and what is the program doing wrong?

I think your intuition seems on, looking at you code, can you see why
that is happening?

Remember, in Python, code blocks are defined by indentation.

-- 
Richard Damon



More information about the Tutor mailing list