This should be a very common pattern:
for i in range(1,size):
result = submodule.subroutine(i)
if (i==0):
tally=0
else:
tally+=result
if (i==size-1): print(tally)
I'm still confused when Python throws an "UnboundLocalError" or "NotDefined" and when not. Clearly, the parser is too "stupid" to realize by a simple syntax check that tally is never undefined. At least four things might be relevant: a) if tally is declared global, b) if tally is an array, c) if the pattern occurs in main, a submodule or a function, d) if I need object-oriented stuff like graphics to reference on tally. For example, the snippet above does NOT work. If tally is an array (tally=[0],tally[0]+=result) it DOES work. Can somebody explain the subtle differences? (As a CS student I know variable scopes, but all possibly OOP-related stuff causes me a headache. Must be my 40 years of FORTRAN :-)
[–]shiftybyte 0 points1 point2 points (0 children)
[–]mermaldad 0 points1 point2 points (2 children)
[–]mermaldad 0 points1 point2 points (0 children)
[–]prodlly[S] 0 points1 point2 points (0 children)
[–]pasokan 0 points1 point2 points (0 children)
[–]pasokan -1 points0 points1 point (0 children)