Code: Select all
while(True):
try:
# File to persist counter
COUNTER_FILE = 'run_counter.txt'
def get_next_element(arr):
"""Get next array element using persisted counter"""
# Read existing counter or initialize
if os.path.exists(COUNTER_FILE):
with open(COUNTER_FILE, 'r') as f:
counter = int(f.read().strip())
else:
counter = 0
# Get current element
element = arr[counter % len(arr)] # Use modulo for circular behavior
# Update counter
counter += 1
with open(COUNTER_FILE, 'w') as f:
f.write(str(counter))
return element
# Example usage across multiple runs
city_array = ['1', '2', '3']
city = get_next_element(city_array)
print(f"Assigned value: {city}")
Code: Select all
print(f"Assigned value: {city}")
break
Code: Select all
city_array = ['1', '2', '3']