Home > Back-end >  why isnt my python code excuting for i in range properly
why isnt my python code excuting for i in range properly

Time:03-15

why is it that for in in range in my code isnt working i want it to run every 7 to 14 seconds but it does less than even 7 seconds

import schedule as s
import webbrowser as wb


def job():
    wb.open ('https://en.wikipedia.org/wiki/Main_Page')
for i in range (7,15):
    s.every(i).seconds.do(job)

X = str(input("Are your ready, yes or no (answer in lower case)"))
if X == "yes":
    job()
else:
    print("put yes!!!")


while True:
s.run_pending()

CodePudding user response:

The way your for loop is currently, it loops 8 times (7...14) and schedules a job each of those times which then will loop every i seconds. To visualize this here is a graphic (_ are seconds and j indicates a job scheduled for that second)

7 :_ _ _ _ _ _ j _ _ _ _ _ _ j _ _ _ _ _ _ j _ _ _ _ _ _ j ...
8 :_ _ _ _ _ _ _ j _ _ _ _ _ _ _ j _ _ _ _ _ _ _ j _ _ _ _ ...
9 :_ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ _ _ ...
10:_ _ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ ...
11:_ _ _ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ _ _ j _ _ _ _ _ _ ...
12:_ _ _ _ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ _ _ _ j _ _ _ _ ...
13:_ _ _ _ _ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ _ _ _ _ j _ _ ...
14:_ _ _ _ _ _ _ _ _ _ _ _ _ j _ _ _ _ _ _ _ _ _ _ _ _ _ j ...

You can see after 14 seconds, 2 jobs have already been scheduled to run on the same second. If you want to schedule a single job to run repeatedly on a random interval check out the schedule documentation on it.

CodePudding user response:

If I understand your code correctly, you want the web page to be opened every X seconds, X being in an interval set between 7 to 15 seconds. The way you're trying to achieve it is just wrong, and I don't think it would ever work. Thankfully, schedule does have a random interval function already made : schedule.every(5).to(10).seconds.do(my_job). To fix your issue, simply replace

for i in range (7,15):
    s.every(i).seconds.do(job)

with

s.every(7).to(15).seconds.do(job)

Now your web page will be opened once every X seconds, with X being a random int between 7 and 15 seconds. Hope this answers your question!

CodePudding user response:

The problem is that you set a bunch of jobs using schedule (one each 7 secs, on each 8, ...) and then run them all at once, so the actual number of jobs run will be much higher. If I understand you right what you actually want is a random wait time between 7 and 14 seconds before running each job.

You can do this easiely without the schedule package (although there might be a method for it as well, not sure):

import random
import webbrowser as wb
import time

def job():
    wb.open('https://en.wikipedia.org/wiki/Main_Page')

X = str(input("Are your ready, yes or no (answer in lower case)"))
while X != "yes":
    X = str(input("put yes!!!"))

while True:
    job()
    time.sleep(random.randint(7, 14))

CodePudding user response:

If you trace your loop, the first iteration will set i to 7, meaning, s.every([7]).seconds.do(job) then 8, 9, and so on. I would suggest doing something like

import random
import time
While True:
   do(job)
   time.sleep(random.randint(7, 14))

Keep in mind that using while True is bad practice, so consider adding a try/catch block in your loop.

Edit: Just noticed you need an interval.

  • Related