+ 1

This code worked but how??

Hey there, I'm currently doing a Crash Course on Python in Coursera offered by Google. There was a question, I didn't know how to solve it so I just tried solving it and accidentally, solved it. I know it sounds stupid but that's what happened with me. So would you please explain me how this code worked?? Fill in the blanks of sum_square function such that it returns the sum of all the square of numbers between 1 & x (not included) def square(n): return n*n def sum_squares(x): sum = 0 for n in range(x): sum += square(n) return sum print(sum_squares(10)) OUTPUT: 285 https://code.sololearn.com/c46mGzz2qgi4/?ref=app

2nd Dec 2020, 6:59 AM
Advik
Advik - avatar
2 Answers
+ 3
You have called sum_square(10). Inside this function, for range(10)( for 0,1,2,... ,9)It calls square(n) function. square (n) returns n*n, hence, sum=sum+square of the particular number. This happens for the range(10). So you got the answer! :-)
2nd Dec 2020, 7:15 AM
Lae
0
I am having a hard time why its doing a sum = sum +Square(n). Confused on the addition . Can any one explain ? when sum = 1 and we are on range 2 sum = 1+2^2 sum = 5 -> But that is not really square(2)
21st Aug 2022, 4:48 PM
Des S
Des S - avatar