title: Toggling rpi3 GPIO pins with a Python script
body: <p>I am a novice programmer.  I learned to code in Basic back in the Commodore 64 days.  Recently, I purchased a Raspberry Pi 3 and an 8-channel relay board and am writing a script in Python 3.x to control the relay board via the GPIO pins.  The script I have written works but I suspect it is not very efficient.  Any critiques or advice would be appreciated.</p>
  
  <p>That being said, I would like to alter the script to toggle the pin's state when it's number is input.  Currently, when the user inputs a number, the pin becomes active for a few seconds then shuts down.  I would like for the pin to remain active until the user chooses the same number again.  I am fairly certain I could code something that will work but, my initial thoughts would be to start another loops that would turn it off again.  Not sure if that makes sense but I AM sure it would not be an elegant or efficiently coded solution.</p>
  
  <p>Any suggestions or help would be appreciated.</p>
  
  <pre><code>import RPi.GPIO as GPIO   # import the GPIO Library
  import time   # import system time
  
  GPIO.setmode(GPIO.BCM)  # set pin numbering 
  
  PinList = [2, 3, 4, 17, 27, 22, 10, 9]   # init list with pin numbers
  
  SleepTime = 4    # set sleep delay variable
  
  for i in PinList:       # loop through pins and set mode and state to 'low' 
  GPIO.setup(i, GPIO.OUT) 
  GPIO.output(i, GPIO.HIGH)
  
  # Get user input, turn on the appropriate GPIO pin, pause, then turn it off.
  
      while True:
      choice = input("\nEnter a number between 1 and 8.\nEnter 0 to exit:  ")
      if choice == '0':
      break
  
      if choice == '1':
      print("You chose 1")
      GPIO.output(9, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(9, GPIO.HIGH)
  
      elif choice == '2':
      print("You chose 2")
      GPIO.output(10, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(10, GPIO.HIGH)
  
      elif choice == '3':
      print("You chose 3")
      GPIO.output(22, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(22, GPIO.HIGH)
  
      elif choice == '4':
      print("You chose 4")
      GPIO.output(27, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(27, GPIO.HIGH)
  
      elif choice == '5':
      print("You chose 5")
      GPIO.output(17, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(17, GPIO.HIGH)
  
      elif choice == '6':
      print("You chose 6")
      GPIO.output(4, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(4, GPIO.HIGH)
  
      elif choice == '7':
      print("You chose 7")
      GPIO.output(3, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(3, GPIO.HIGH)
  
      elif choice == '8':
      print("You chose 8")
      GPIO.output(2, GPIO.LOW)
      time.sleep(SleepTime)
      GPIO.output(2, GPIO.HIGH)
  
      else:
      print("\nThat is not a valid input.")
  
  print ("\n  Quit")
  
  GPIO.cleanup()  # Reset GPIO pin settings
  </code></pre>