[Tutor] for Loops with Squared
Alan Gauld
alan.gauld at yahoo.co.uk
Sat May 23 13:49:57 EDT 2020
On 23/05/2020 16:08, Mary Knauth via Tutor wrote:
> calculate the sum of each value of N squared from 0 up to and including N.
>
> 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.
Since you don't tell us what is going wrong its hard to know.
The code above looks like it should work and print(for 3)
0
1
5
14
If that's not what you get - or not what you expected - then tell us
what is happening.
If you get an error message send the complete error message.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list