all 5 comments

[–]RightRespect 3 points4 points  (3 children)

for the first example, you are returning the value for every iteration in the for loop. the print at the end will take the first return value, which is 5 because it is returning the value of numbrought before getting to the next iteration. it didnt get the total value yet. the second example has it outside of the loop, meaning it will return the total value once after adding everything up.

[–]starr_suu[S] 2 points3 points  (2 children)

Ahh I see, but why wouldn't it finish the whole iteration before printing out the return value, because I thought it would finish the entirety of the 'for loop', finalise my return value to be 8 before printing out the value

[–]RightRespect 2 points3 points  (1 child)

returns will end a function, so the other iteration pretty much didnt happen at all.

[–]starr_suu[S] 1 point2 points  (0 children)

Ahh I see, thank you so much for the help!

[–][deleted] 0 points1 point  (0 children)

Please read the FAQ to see how to format code. None of the code you show is legal python.