Is there a way to reset a value to its initial state. For example, if a=5 and then throughout the program a-=1 continuously until a=0. How would I reset the program so a=5 again?
Asked
Active
Viewed 939 times
1 Answers
0
Variables don't remember their initial (or any previous) value. You'll need to store that somewhere else.
initial_a = 5
a = initial_a
...
a = initial_a if a == 0 else a - 1
...
You might, however, want a generator that produces an infinite stream of repeating values.
import itertools
a_values = itertools.cycle([5,4,3,2,1,0])
a = next(a_values) # a == 5
a = next(a_values) # a == 4
a = next(a_values) # a == 3
a = next(a_values) # a == 2
a = next(a_values) # a == 1
a = next(a_values) # a == 0
a = next(a_values) # a == 5
# etc

chepner
- 497,756
- 71
- 530
- 681