Python Forum
loop with timer help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: loop with timer help (/thread-27089.html)



loop with timer help - Gina92 - May-25-2020

Hi all I'm extremely new to python and have just completed the online code academy course and I'm trying to create a code that will generate a random word from a set list and run through them at 30 second intervals until complete.

I cannot work out how to do the loop and timer i know this is probably the simplest thing going but as i say I'm extremely new and am looking for some advise.

I have searched for the answer and cant see anything any guidance would be appreciated.

TIA


RE: loop with timer help - bowlofred - May-25-2020

Can you go ahead and show your attempt (even a completely broken one)? It helps understand the level you're at. Are you using time.sleep()?

Do you want a random word from a list (which means sometimes the words might repeat), or do you want to create a shuffled version of the list (so when you run through it, there are no repeats)?


RE: loop with timer help - Gina92 - May-25-2020

Hi i am looking for it to randomly print the words with no repeats - all i have so far is the below. I have also come onto the issue that the programme is repeating the same words multiple times rather than running through the list.

import random
import threading

words = ["roll left", "pull", "duck", "roll right", "slip"]
word = random.choice(words)


def action():
        print(word)


my_timer = threading.Timer(5.0, action)
my_timer.start()



RE: loop with timer help - snippsat - May-26-2020

(May-25-2020, 08:58 PM)Gina92 Wrote: issue that the programme is repeating the same words multiple times rather than running through the list.
Use random.sample for this,and don't see the need for threading here.
import random
import time

words = ["roll left", "pull", "duck", "roll right", "slip"]
for choice in random.sample(words, len(words)):
    time.sleep(5)
    print(choice)