Recently I used this to toggle a Boolean value, b being the current value and self.status being the result
self.status = (b-1)*(b-1)
This rather than use an if statement
How could I use the same concept to toggle more than 2 values? (ie -1 0 1)
Recently I used this to toggle a Boolean value, b being the current value and self.status being the result
self.status = (b-1)*(b-1)
This rather than use an if statement
How could I use the same concept to toggle more than 2 values? (ie -1 0 1)
You can cycle through the numbers $0,\dotsc,n-1$ by adding $1$ and taking the remainder modulo $n$:
$$ k\to(k+1)\bmod n\;. $$
Most programming languages have an operator for taking the remainder; in C-like syntax this would be k=(k+1)%n.
If you want to cycle through $a,\dotsc,b$ instead, just shift by $a$ before and after the operation:
$$ k\to((k - a+ 1)\bmod (b-a+1))+a\;. $$
In your case, with $a=-1$ and $b=1$, this would be
$$ k\to((k +2)\bmod 3)-1\;. $$
For 'toggling' $-1,0,1$ use $$x\mapsto -x$$
In general, for $a,a+1,a+2,..,b$, use $$x\mapsto (a+b)-x$$
1-bwould have been simpler for two values.) – joriki Oct 19 '12 at 10:38