Edit: Thank you very much guys. This code is finally working:
# imports
from pynput.keyboard import Key,Controller
import time
import threading
keyboard = Controller()
a=int(input("Enter the number of loops to send: "))
arr=["pls beg","pls fish","pls hunt","pls postmemes","c","pls search","hospital", "pls highlow", "high", "pls trivia", "C", "pls deposit all"]
print("Please take the cursor where you want to spam the message, spamming will start in 5 seconds...")
time.sleep(5)
num=0
def doFunc(function, timeToSleep):
for num in range(a):
# we pass functions because the class is not initialised
function(functions, timeToSleep)
# the function with classes
class functions:
# example functions
def function1(self, timeToSleep): #pls beg command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[0])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function2(self, timeToSleep): #pls fish command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[1])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function3(self, timeToSleep): #pls hunt command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[2])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function4(self, timeToSleep): #pls postmemes command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[3])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[4])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function5(self, timeToSleep): #pls search command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[5])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[6])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function6(self, timeToSleep): #pls highlow command (cooldown of 20s)
time.sleep(timeToSleep)
keyboard.type(arr[7])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[8])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function7(self, timeToSleep): #pls trivia command (cooldown of 10s)
time.sleep(timeToSleep)
keyboard.type(arr[9])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[10])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function8(self, timeToSleep): #pls deposit all command (cooldown of 45s)
time.sleep(timeToSleep)
keyboard.type(arr[11])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
# change the times you want each function to sleep
timesDict = {
functions.function1: 32,
functions.function2: 31,
functions.function3: 33,
functions.function4: 34,
functions.function5: 35,
functions.function6: 21,
functions.function7: 11,
functions.function8: 45
}
# start threads
for f, t in timesDict.items(): threading.Thread(target=doFunc, args=(f, t)).start()
How can I make a python program for doing a few tasks repeatedly for same number of times (can use a simple for loop here) but each having independent time intervals? For example, I want function 1 to be executed every 30 seconds, function 2 to be executed every 17 seconds, function 3 to be executed every 36 seconds, and so on. I tried doing it using time.sleep(), but I couldn't make it work since their time intervals are independent of each other.