#Loop help

1 messages · Page 1 of 1 (latest)

normal junco
#

a practice quiz. the answer is 26 when i run it in thonny ,but im getting 14.

chat gpt also gave me 14 (i know its not right most of the time)

sum_n = 0
i = 0
done = False
while not done:
sum_n += i
i += 1
j = i
while j>0:
sum_n += 2
j -= 1
if i >= 4:
done = True

What is the value of sum_n at the end of the loop?

buoyant marsh
#
i=1 sum_n=2
i=2 sum_n=7
i=3 sum_n=15
i=4 sum_n=26
#

I'm not sure which step you're missing to get 14

formal siren
#

igot 26 too. how are you running it?

#

are you printing out sum_n? or is this part of a function?

buoyant marsh
#

I think they're running it and getting 26 but don't understand the logic and when they walk through the code in their head they get 14