+ 1

python creating math functions by function

so here is a code of python which exactly i couldn't get how it's done when u put 5 the result comes 40 , but how ? def Func(number): total = 0 while number > 0: total = total + number*(number-1) number = number -1 return total

26th Nov 2017, 2:29 PM
Atiq Mayar
Atiq Mayar - avatar
2 Respuestas
+ 1
It is: 5 * 4 + 4 * 3 + 3 * 2 + 2 * 1 + 1 * 0 Then number is 0 and the while loop finishes. The first number on each line is the value for number.
26th Nov 2017, 3:10 PM
Paul
Paul - avatar
+ 1
total = (0)+ 5*4 total = (5*4)+4*3 total = (5*4+4*3)+3*2 total = (5*4+4*3+3*2)+2*1 total = (5*4+4*3+3*2+2*1)+1*0 the return statement is used outside the while loop thus you get a output of 40 at last
26th Nov 2017, 3:41 PM
VISHNU P.S
VISHNU P.S - avatar