todd2323
todd2323

Reputation: 67

Simple python and need explanation

m = 0
for x in range (4,6):
   for y in range (2,4):
      m = m + x + y
print (m)

ANSWER: 28

not sure how this is? Excluding the last number in the range, I thought it should be 14. I add it up on paper and cannot understand what I am doing wrong.

Upvotes: 3

Views: 30

Answers (1)

John1024
John1024

Reputation: 113844

That loop is equivalent to:

m = 4+2 + 4+3 + 5+2 + 5+3

And, that sum is 28.

(In the outer loop, x takes on the values 4 and 5. In the inner loop, y takes on values 2 and 3.)

Upvotes: 4

Related Questions