def square( first, last ):
total = 0
for i in range( first, last + 1 ):
total += i**2
return total
print square( 3, 5 )

thankyou very much for the help, but could you please explain the last 2 lines.. coz we have not done tht yet and there is want to show work which i cant explain..

total holds the sum of all squared values, so the first time i = 3 so i**2 = 9, and total += i**2 means total = total + 9. The value for total here is 0 so total = 0 + 9 and now total is 9.
The second time i**2 = 16 so total = total + i**2 is 9 + 16 and therefore total = 25 now.
And the third time i**2 = 25 so total = 25 + 25 and total = 50

All we need to do now is to return it
return total

and that's it.

Hope this helps :)
If not I'll try to explain it better :)

def square( first, last ):
total = 0
for i in range( first, last + 1 ):
total = i**2 # Not total += i**2
return total
print square( 3, 5 )

it would output this:

>>>
25
>>>

because you are not adding the result of every square to total, i.e total becomes whatever the result for the last value of i squared is, since i is 5 at the end, total becomes i**2 which is 25 :)

yup thts exactly wht i meant... and + would mean it squares every number and add it to the total and moves on the next without leaving any out.. yup tx for the help... appreciate it... nw i get it..

but one last question, why is the total=0? is tht like a rule that it has to equal 0?

yup thts exactly wht i meant... and + would mean it squares every number and add it to the total and moves on the next without leaving any out.. yup tx for the help... appreciate it... nw i get it..

but one last question, why is the total=0? is tht like a rule that it has to equal 0?

well when you want to count stuff or find a sum of numbers you need the count, or whatever variable name you use, to be initially 0 because if it's 1 or more or 0 and less you will get incorrect results. Basically this is what I mean:

>>>total = 1
>>>total = total + 5**2
>>>print total
26 # but 5**2 is 25 so total has to be 0

the statement total = total + 5**2 means compute the sum of the current value of total and the square of 5 and replace the current value of total with the result of the computation :)

Hello guys, hope you all fine,,
I have a problem when coding using python and opencv.
My project is to track the ball in model soccer pitch using raspberry pi ...