Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop with timer help
#1
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
Reply
#2
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)?
Reply
#3
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()
Reply
#4
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  email timer/rss feed timer ndiniz 1 2,045 Feb-02-2021, 07:18 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020