Problem with the use of the while statement


#1

Hi,
Somebody should please help me with this below codes. I expected the program to print the total for the sum of square of integers(starting from 1). The language is Pyhton
def square():
sum = 0
total = 0
i = 1
while i <= 10:
square = i^2
sum += square
i += 1
total = total + sum
print(total)

square()

This is the output is bringing out

3
6
10
20
37


#2

In python we have ** that does work of exponential operator.
Examples..

print 2**3 # will print 8
print 10 ** 2 # will print 100

Note: ^ is not an exponent operator but bitwise operator.


#3

Do we need two accumulators? Our goal is always to write the least amound of code to keep things from getting complicated and confusing.

i = 0
total = 0
while i < 10:
    i += 1
    total += i ** 2
return total

#4

Thanks bro, but I still expect the program to print only the total of the sum of square of integers.
But the program brought this output.

1
6
20
50
105


#5

I'm still waiting for someone who will help me to fix this problem.
Kind Regards


#6

Show us what you have for code, please. As well, please post a link to the lesson when asking for help. It makes it so much easier when we know the exact instructions and can test your code.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.