1

I want to write a program that prints all the elements in a list over and over again with 5 seconds of interval between each element. So if the list is [1,2,3] it would print first the 1, then wait 5 seconds, print the 2 wait 5 seconds, print the 3 wait 5 seconds,then go back to printing the 1 and waiting 5 seconds, etc...

This is the code I have to illustrate how I'm thinking but this doesn't work at all. I think I have to use the threading library because the program will be doing some repetitive tasks parallel to printing the elements in this list. '''

import threading

def func(list, i):
    print(list[i])
    if i < 4:
        i+=1
    else:
        i = 0
    threading.Timer(1,funcao, list, i).start()


list = ['1', '2', '3', '4']
func(list,0)

'''

How can I build such a program? (btw I'm new to programing so I'm sorry if this questions is basic but I coulnd't find any answers for this)

  • What is your question, can you be more specific? – AMC Apr 12 '20 at 20:57
  • May be related: [Making async for loops in Python](https://stackoverflow.com/questions/53486744/making-async-for-loops-in-python) ... [What is the best way to repeatedly execute a function every x seconds?](https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds) - threading answers there ... – wwii Apr 12 '20 at 21:20

3 Answers3

1

you could use itertools.cycle to cycle your list and a while loop to forever continue the print process:

    from itertools import cycle
    import time

    def func(my_list):

        c = cycle(my_list)
        while True:
            print(next(c))
            time.sleep(5)

    func(['1', '2', '3', '4'])  

if you want to do other tasks in parallel you can use:

from itertools import cycle
from threading import Thread
import time

def func(my_list):

    c = cycle(my_list)
    while True:
        print(next(c))
        time.sleep(5)

t = Thread(target=func, args=(['1', '2', '3', '4'], ))
t.start()

# other code
# to test you may use:
# time.sleep(30)
kederrac
  • 16,819
  • 6
  • 32
  • 55
0

I'm not 100% sure what you require with regards to the threading? But, if all you're after is a function that will take a list and repeatedly print the elements in the list one by one (indefinitely) whilst waiting a pre-defined number of seconds between each print, this function should do the trick.

You can use the sleep() function from the time module. The sleep() function suspends (waits) execution of the current thread for a given number of seconds.

import time

def func(my_list, seconds): 
        while True: 
            for item in my_list: 
                print(item)
                time.sleep(seconds)

Then you can just do something like:

my_list = [1,2,3]
seconds = 5 
func(my_list, seconds)
d_s_m
  • 67
  • 5
  • thanks, but my program will be doing other things parallel to printing out the elements in a list. that is why i need the threading library. with your code it would never leave the "func" function and i need the program to do other things – Miguel Carvalho Apr 12 '20 at 21:16
0

Try this.

import time

def func(lst):
    for x in lst:
        print(x, end=" ")
        time.sleep(5)
    func(lst)

func([1,2,3]) 

It's better to have stopping condition so you can modify it like that

import time

def func(lst,i):

    if i < 0:
        return

    for x in lst:
        print(x, end=" ")
        time.sleep(5)
    time.sleep(5)
    i -= 1
    print()
    func(lst,i)

func([1,2,3],2)
losifar luv
  • 105
  • 3