title: Python3 toggle script on key press stuck
body: <p>The goal of this script is to make a switch and let the user toggle it ON and OFF with a key press, and when the switch is ON, the script should execute a loop that print a message in the terminal. In another words, <strong>The goal is to repeatedly print a message when the switch is ON</strong></p>
  
  <p>Here is what i have tried:</p>
  
  <pre><code>import keyboard
  import time
  
  switch = 0    # The switch variable
  
  
  def check_start():
      global start
      if keyboard.is_pressed("F5") and start == 0:    # If F5 is pressed, turn ON print message loop
          switch = 1
          print(switch)
          time.sleep(0.1)    # This is to prevent the user toggling the switch too fast
  
      if keyboard.is_pressed("F5") and start == 1:     # If F5 is pressed again, turn OFF print message loop
          switch = 0
          print(switch)
          time.sleep(0.1)
  
  
  def print_name():    # If the switch is ON, it should print two seperate message with a 10 seconds interval
      if start == 1:
          print("start")
          time.sleep(10)
          print("end")
  
  
  while True:
      check_start()
      print_name()
  </code></pre>
  
  <p>Here is the output of the script:</p>
  
  <pre><code>1
  start
  end
  start
  end
  0
  1
  start
  end
  0
  </code></pre>
  
  <p>Now here's the problem:</p>
  
  <p><strong>The user cannot turn off the switch while the print loop is in progress.</strong> For example the user cannot turn off the script if the message "end" has not printed, the user can ONLY turn off the switch exactly after "end" has printed, and if the user has missed the opportunity to turn off the switch, he must wait 10 more seconds to turn it off. <strong>What i expected is the user can toggle On and OFF anytime s/he wishes to.</strong></p>
  
  <p>Is this possible to do in without importing too much module?</p>