<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

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 jid