how could i square the result of function?.. so when i call the functioon

def sum(first, last):
    for i in range(first, last+1):
        print i
        return i**2

it gives the answer as 50, as it should square 3,4 and 5=50

Recommended Answers

All 10 Replies

Member Avatar for masterofpuppets

hi,
here's a way of doing it:

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

print square( 3, 5 )

hi,
here's a way of doing it:

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

Member Avatar for masterofpuppets

right,

here's what happens here:

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 :)

Member Avatar for masterofpuppets

P.S.
probably the '+=' operation is confusing you here.... total += 1 simply means total = total + 1

:)

P.S.
probably the '+=' operation is confusing you here.... total += 1 simply means total = total + 1

:)

so if the + operation was not there then would it have outputted 9? because it would just do 3**2 and not add the rest?

Member Avatar for masterofpuppets

so if the + operation was not there then would it have outputted 9? because it would just do 3**2 and not add the rest?

yes but at the end of the loop you would get 25 because the last value of i is 5, i.e. 5**2 = 25 so the value of total becomes 25

P.S.
probably the '+=' operation is confusing you here.... total += 1 simply means total = total + 1

:)

yes but at the end of the loop you would get 25 because the last value of i is 5, i.e. 5**2 = 25 so the value of total becomes 25

so would that mean it would do 3**2, 4**2 and 5**2 but it would add 9+16+25 and instead it would just output 25 as it at the end of the loop?

Member Avatar for masterofpuppets

if you write the function like this:

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?

Member Avatar for masterofpuppets

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.