Home > Blockchain >  How do I use a variable outside of a while loop
How do I use a variable outside of a while loop

Time:11-03

When I run the script, there are no errors but nothing gets printed nor logged, even if a model is actually uploaded.

import os
import requests
import sys
from colorama import Fore

os.system('cls')

while True:
    info = requests.get("https://search.roblox.com/catalog/json?CatalogContect=2&Category=6&SortType=3&ResultsPerPage=1").json()


if info[0]['Name'] == "DreamGrim":
        pass
elif info[0]['Name'] == ".gg/Fgj3VRGapz":
        pass
else:
    print(f"[{Fore.LIGHTGREEN_EX} {Fore.RESET}] Logged model: "   str(info[0]['Name'])   " | " "https://www.roblox.com/library/"   str(info[0]['AssetId'])   "/"   str(info[0]['Name']))
    with open('logs.txt', 'a') as f:
        f.write(f"\n[ ] Logged model: "   str(info[0]['Name'])   " | " "https://www.roblox.com/library/"   str(info[0]['AssetId'])   "/"   str(info[0]['Name']))

How do I make this work without the logs getting looped?

If I put it inside the while True: loop the logs start to loop and something like this happens:

[ ] Logged model: Hot Chocolate | https://www.roblox.com/library/7881318998/Hot Chocolate
[ ] Logged model: Hot Chocolate | https://www.roblox.com/library/7881318998/Hot Chocolate
[ ] Logged model: Hot Chocolate | https://www.roblox.com/library/7881318998/Hot Chocolate
[ ] Logged model: Hot Chocolate | https://www.roblox.com/library/7881318998/Hot Chocolate
[ ] Logged model: Hot Chocolate | https://www.roblox.com/library/7881318998/Hot Chocolate

CodePudding user response:

You have to terminate the while loop before the script can progress to the next step. If you try and run the following code you will get repeated 1s and no 2s:

while True:
    print(1)
print(2)

A better way of structuring the code would be to get the info first and then iterate through it:

info = requests.get("https://search.roblox.com/catalog/json?CatalogContect=2&Category=6&SortType=3&ResultsPerPage=1").json()

for result in info:
    if result['Name'] == "DreamGrim":
        pass
        ...

CodePudding user response:

You want to use the while True statement for something that should loop until you reach a point. Here, you are only creating and changing your variable's value over and over again. You never break off of the loop so you never get to the rest of the code.

Maybe you wanted to use the with statement ?

  • Related