Reputation: 17
I'm trying to make the program choose one of the items from the list, print it and repeat the process again and again.
I tried to create a boolean for the loop and the time.sleep doesn't seem to do something.
import random
import time
sounds=["Kick", "Hi-Hat", "Snare"]
beat=random.choice(sounds)
while True:
print(beat)
time.sleep(0.5)
It was supposed to print random items infinitely with a sleep of half a second but every time I run the program it just picks a random item and prints it again and again really fast (sorry for the bad English, I'm Portuguese).
Upvotes: 0
Views: 81
Reputation: 713
You are only selecting the random item once and then running the loop. Try putting the random function inside the loop like this:
while True:
beat=random.choice(sounds)
print(beat)
time.sleep(0.5)
If it's faster than 0.5 seconds in your code it's probably because you've indented it bad and only the print
statement falls inside the while
loop. Be sure to indent everything that should be inside the loop with 4 spaces like I did here.
Upvotes: 1