Why does idcter not reset to 0 when it exceeds maxid?
maxid=9999
idcter=9999
idcter += 1 if(idcter <= maxid) else 0
print('this is good: ' + str(idcter))
idcter += 1 if(idcter <= maxid) else 0
print('now this is weird: ' + str(idcter))
idcter=10000
idcter = idcter + 1 if(idcter <= maxid) else 0
print("that's better: " + str(idcter))
Output:
this is good: 10000
now this is weird: 10000
that's better: 0
So it's a simple fix, but why would idcter not reset after it exceeds maxid?
+= 0will do the same thing as= 0? If it did, that would make+=very unpredictable and hard to use…idcter += 1 if idcter <= maxid else 0to set the variableidcterto 0. Even if that statement would be parsed as(idcter += 1) if idcter <= maxid else 0, neither of the twoifbranches assigns 0 toidcter. Theidcter += 1branch increments it, and the0branch does nothing. There is no assignment in that branch.