code: def select_joystick():
      '''Allow user to select a joystick from a menu'''
  
      list_joysticks()
  
      while True:
          print('Select a joystick (L to list, Q to quit)'),
          choice = sys.stdin.readline().strip()
  
          if choice.lower() == 'l':
              list_joysticks()
          elif choice.lower() == 'q':
              return
          elif choice.isdigit():
              jid = int(choice)
              if jid not in range(pygame.joystick.get_count()):
                  print('Invalid joystick.')
                  continue
              break
          else:
              print('What?')
  
      return jif select_joystick():
      '''Allow user to select a joystick from a menu'''
  
      list_joysticks()
  
      while True:
          print('Select a joystick (L to list, Q to quit)'),
          choice = sys.stdin.readline().strip()
  
          if choice.lower() == 'l':
              list_joysticks()
          elif choice.lower() == 'q':
              return
          elif choice.isdigit():
              jid = int(choice)
              if jid not in range(pygame.joystick.get_count()):
                  print('Invalid joystick.')
                  continue
              break
          else:
              print('What?')
  
      return jid
docs: Allow user to select a joystick from a menu