Python Homework - sum of perfect squares -
i need python homework question.
"assume there variable , h associated positive integer value. write code necessary compute sum of perfect squares value less h, starting 1. (a perfect square integer 9, 16, 25, 36 equal square of integer (in case 3*3, 4*4, 5*5, 6*6 respectively).) associate sum compute variable q. example, if h 19, assign 30 q because perfect squares (starting 1) less h are: 1, 4, 9, 16 , 30==1+4+9+16."
so far close having right 1 number needs. example putting in 19, instead of stopping @ 1,4,9,16 adds 25 too.
heres code far
h_i=input() h=int(h_i) s=0 q=0 total=s**2 while total<=h: s+=1 total=s**2 q+=total print(total) print(q)
you need put s+=1 , total=s**2 @ end of loop, condition (which believe should total<h) checked before it's added q.
h_i=input() h=int(h_i) s=0 q=0 total=s**2 while total<h: q+=total print(total) s+=1 total=s**2 print(q)
Comments
Post a Comment