-1

Hello i have a loop its basically a name generator and i want to stop it at some point.I had the idea to stop it with a key.Because i cant stop it due to its high rate of generating words i cant input stop or anything its too fas.Like if i press Enter the generator should stop but i couldnt figure out how.And i had the idea of a timer when the generator generated 1000 words it would stop.All timers stopps/timers are accepted. here is mycode `

import time 

 from time import sleep

 import random 
 import string

def run_bot():
    x1 = random.choice(string.ascii_uppercase)
    x2 = random.choice(string.ascii_lowercase)
    x3 = random.choice(string.ascii_lowercase)
    x4 = random.choice(string.ascii_lowercase)
    x5 = random.choice(string.ascii_lowercase)
   name = str(x1 + x2 + x3 +x4 +x5)

    print(name)


while True:
    for i in range(5):
        run_bot()           

`

bot_diyar
  • 80
  • 7

1 Answers1

1

You could use multithreading.

Have a global varaible called something like stop:

stop = False

And replace your while True with while not stop.

Then you can create a function that waits for the user to press enter and call it in a new thread so it does not interrupt your name generation:

from threading import Thread

def wait_for_stop():
    input()
    stop = True

Thread(target=wait_for_stop).start()

Just call those lines before entering the while loop and everything should be working fine.

Tim Woocker
  • 1,883
  • 1
  • 16
  • 29