How to open different web pages using while

Asked

Viewed 131 times

1

I would like the program to perform the repetition of a command 3 times: wait 10 seconds to open a web page. For this repetition to occur I set the code below:

import time
import webbrowser

total_breaks = 3
break_count = 0

while(break_count < total_breaks):
    time.sleep(10)
    webbrowser.open("https://www.youtube.com/watch?v=zywDiFdxopU")
    webbrowser.open("https://www.youtube.com/watch?v=QwOU3bnuU0k")
    webbrowser.open("https://www.youtube.com/watch?v=b2WzocbSd2w")
    break_count = break_count + 1

But it turns out the pages open at the same time. I would like the first time he opens page a and the second time he opens page b and the third time he opens page c. How do I so that instead of the program opening all the web pages, he opens a page every time the program runs?

1 answer

1


For each open has a sleep correspondent. This already gives you a clue that your loop cannot be composed of several open the way it is, and yes only one.

How will you have just one open, you need to vary its parameter. As the possible parameters are fixed, let’s put them in a tuple and iterate over it with a for instead of a while:

import time
import webbrowser

enderecos = ("https://www.youtube.com/watch?v=zywDiFdxopU", "https://www.youtube.com/watch?v=QwOU3bnuU0k", "https://www.youtube.com/watch?v=b2WzocbSd2w");

for endereco in enderecos:
  webbrowser.open(endereco)
  time.sleep(10)
  • Pablo, I tried to run this way, but a syntax error appears. Yours ran smoothly?

  • I am without access to a Python interpreter now, but the change I made should not have been a mistake. What is the message and on which line is?

  • I think now it will. Try again there.

  • The message is: "Traceback (Most recent call last): File "python", line 6 for (endereco in enderecos): Syntaxerror: invalid syntax". It points specifically to the two points.

  • 1

    It had parentheses where it shouldn’t. Try again without them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.