dimanche 5 avril 2020

How do I open a browser only once inside a for loop?

The code is:

import requests
from time import sleep
import webbrowser
from termcolor import colored


print(colored('Lowest Priced Limited\n---------------------\n', 'green'))

while True:
   lowestprice = 1234567890
   for limited in requests.get('https://search.roblox.com/catalog/json?Category=2&Subcategory=2&SortType=4&Direction=2').json():
       price = int(limited['BestPrice'])
       if price < lowestprice:
           limitedname = limited['Name']
           limitedurl = limited['AbsoluteUrl']
           lowestprice = price
   print(colored(f"{limitedname}: {lowestprice}\n{limitedurl}\n"))
   sleep(1)

   if lowestprice <= 300:
      webbrowser.open(limitedurl, new=2)

As you can see, the last if statement opens the URL. However, since it is inside a for loop, it keeps reopening over and over again. How do I make it open only once, while keeping the rest of the code still going? Not breaking the whole code and opening the URL, but opening the URL once, and keeping the code going.

Aucun commentaire:

Enregistrer un commentaire