1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why does my code say break outside loop when it is in the while true/if: statement?...

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    Whenever I try to run this code, it results in me getting the error: break outside loop. What is wrong?

    import time
    import keyboard
    import random

    def wait():
    wait_time = random.randint(1, 20)
    print(wait_time)
    time.sleep(wait_time)
    print('go')
    while True:
    start_time = time.time()
    while True:
    elapsed_time = time.time() - start_time
    print(f"Elapsed time: {elapsed_time:.2f} milliseconds", end='\r')
    time.sleep(0.01)
    print(keyboard.read_key())
    if keyboard.read_key() == "a":
    break

    print("press the key 'a' when it says go")
    wait()

    Continue reading...

Compartilhe esta Página