Search code examples
pythonarraysfunctiondatetimereturn

Issuing array elements every 2 hours python


There is an array in the function: array = ['elementOne', 'elementTwo' etc].

We need to return the next element every two hours. How to implement this?

Example: returns elementOne, then after two hours the next element. As soon as there is the last element, it starts over.


Solution

  • The for loop is where you will put the code that you want to execute using that element.

    The while loop is there to repeat the process after the array is completed. The for loop goes through each item in the list. The sleep waits two hours before the next item.

    from time import sleep
    
    while True:
        for val in array:
            #Your Code Here that uses the value
    
            sleep(5200)
    

    Originally had generator function, but realized it wasn't needed.