Home > OS >  i want to make a loop, for every x actions do y
i want to make a loop, for every x actions do y

Time:11-28

i want to make a loop, to run x times, and when it runned x times, then do y and start again to run x time like first time.

This is what i tried, but run only 1 time (x), and then did (y) but i need to run 20 times (x) and only after 20 times do y and start again to do (x)

                    for user in usernames:
                    print('Sending Commnet!...')
                    
                        try:
                            time.sleep(1)
                            driver.find_element(
                                By.XPATH, comment_section).send_keys(f'@{user} ')
                            time.sleep(1)
                            mentiune = mentiune   1
                            if f'@{user}' in driver.page_source:
                                time.sleep(0.5)
                                print(
                                    f'{Fore.GREEN} {user} --> Added to comment Successfuly! -- {datetime.now()}  -- {Fore.RESET} \n')
                                x = random.randint(0.5, 1)
                                print(f'{str(x)} secound sleeped!... \n')
                                time.sleep(x)
                            else:
                                print(f'{username} is Reaport')
                                continue
                                
                        except:
                            pass
                        
                    
                driver.find_element(By.XPATH, send_button).click()

So, i need, to get from "usernames" line by line "user" and type in a comment "@ user " 20 times, and then click "send_button" and then type again 20 users, but not from the start of the list.

CodePudding user response:

I'm not an expert programmer but try if this works:

def generator(usernames):
    for user in usernames:
        yield(user)

def my_func(usernames):
    generator_obj = generator(usernames)
    try:
        while True:
            for _ in range(20):
                user = next(generator_obj)
                # do something with user
            # do y
    except StopIteration:
        pass

CodePudding user response:

I inserted here the full code.

[The full code][1]


  [1]: https://pastebin.com/dc0NPat7
  • Related