how to i get python schedule to loop through my task

Viewed 48

please help me debug this code please. i have list of users which i want them to be doing different task as wanted. i want this users to be able to start this task at a scheduled time but the issue here is i cant loop through the users. if i dont use schedule, the code runs as i want but when i use schedule it only output just one user at that particular time hopefully will output another the next time. please help here, thats my code below

import random
import schedule as s
import time


Activities = ['eating', 'coding', 'praying', 'reading']


class now:
    def __init__(self, name):
        self.name = name

    def get_name(self):
        return self.name

    def start(self):

        print('My name is', self.name)

        a = random.choice(Activities)
        if a == 'eating':
            print('i am eating..')
        elif a == 'coding':
            print('I am coding')
        elif a == 'praying':
            print('I am praying')
        else:
            print('I am visit: reading')

user1 = now('king')
user2 = now('chibuike')
user3 = now('ogodo')
user4 = now('kingsley')
user5 = now('sunday')
user6 = now('success')
user7 = now('emeka')
user8 = now('jekwu')
user9 = now('obinna')

all = [user1, user2, user3,user4, user5, user6, user7, user8, user9]
random.shuffle(all)


for i in all:
    s.every().day.at("13:11").do(i.start)
    while True:
       s.run_pending()

I want the code to be able to loop through and output all the users with their activities at the scheduled time not just one user at a time.

1 Answers

Try moving your while loop above the for loop like this:

while True:
    for i in all:
        s.every().day.at("13:45").do(i.start)
        s.run_pending()

You may need to add a time.sleep(1) after s.run_pending() but I have not tested that.

Related