[Tutor] for Loops with Squared

Mary Knauth maryknauth at mac.com
Sat May 23 15:20:44 EDT 2020


Hi All, 

This is an amazing support community!

@Richard - you are correct, indentation IS EVERYTHING
The print() function needed to be outside the loop.

This is for a scripting class at SNHU...

Thanks!

Warm Regards,

Mary Knauth




 <https://zephyrsolutions.us/>  

> On May 23, 2020, at 14:45, alexkleider <alexkleider at protonmail.com> wrote:
> 
> 
> ‐‐‐‐‐‐‐ 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