Пример #1
0
def get_answer_value(valid_answers, cur=None):
    print('Valid Answers:')
    if all([isinstance(k, int) for k in valid_answers]):
        for k, v in enumerate(valid_answers):
            if k == cur:
                print('[%d]' % v)
            else:
                print(' %d ' % v)
        if cur is None:
            choice = int(input('Enter a value: '))
        else:
            choice = int(ifinput('Enter a value: ', valid_answers[cur]))
        try:
            choice = [k for k, v in enumerate(valid_answers) if v == choice][0]
        except IndexError:
            return -1
    else:
        for k, v in enumerate(valid_answers):
            if k == cur:
                print('[%d: %s]' % (k+1, v))
            else:
                print(' %d: %s ' % (k+1, v))
        if cur is None:
            choice = int(input('Select an answer 1-%d:' % len(valid_answers)))
        else:
            choice = int(ifinput('Select an answer 1-%d:' % len(valid_answers), cur+1))
        choice -= 1
    return choice
Пример #2
0
def get_selector():
    sel = None
    while sel not in selectors:
        print('What kind of target would you like to study?')
        for i in selectors:
            print('[%s]%s' % (i[0], i[1:]))
        choice = input('Enter choice: ').lower()
        sels = [k for k in selectors if k[0].lower() == choice]
        if len(sels) == 1:
            sel = sels[0]
    return sel
Пример #3
0
def cyoa(prompt, valid, default=None):
    i = ''
    while True:
        if default is not None:
            i = ifinput('%s [%s]' % (prompt, valid), default)
        else:
            i = input(prompt)
        if i.lower() in valid.lower():
            break
        try:
            int(i)
            break
        except ValueError:
            pass
        print('invalid choice')
    return i.lower()
Пример #4
0
def pick_one_or(items, default=None):
    print('\nChoice Item')
    print('%s %s' % ('=' * 6, '=' * 70))

    if items is not None and items != []:
        field_width = ceil(log10(len(items)))

        for i, k in enumerate(items):
            print(' [%*d]%s %s' % (field_width, i, ' ' * (3 - field_width), k))
    if default is not None:
        c = ifinput('or enter new value: ', default)
    else:
        c = input('or enter new value: ')
    try:
        choice = items[int(c)]
    except ValueError:
        choice = c
    return choice
Пример #5
0
def _pick_list(items, *args, prompt=None):
    """
    enumerates items and asks the user to pick one. Additional options can be provided as positional
     arguments.  The first unique letter of positional arguments is chosen to represent them. If no unique
     letter can be found, something else can be done.

    Returns a, b: if the user selected an item from the list, a = the enumeration index and b = None.
    If the user selected one of the alternate options, a = None and b = the option selected.

    A null entry is not allowed, but if the user types and enters 'None', then None, None is returned.

    options are not case sensitive.

    Example: _pick_list(['one cat', 'two dogs', 'three turtles'], 'abort', 'retry', 'fail')
    will present a menu to the user containing options 0, 1, 2, A, R, F and will return one of the following:
    (0, None)
    (1, None)
    (2, None)
    (None, 'abort')
    (None, 'retry')
    (None, 'fail')
    (None, None)
    :param items:
    :return:
    """
    menu = []
    for arg in args:
        letter = (k.upper() for k in arg + ascii_uppercase)
        mypick = None
        while mypick is None:
            j = next(letter)
            if j not in menu:
                mypick = j
        menu += mypick

    print('\nChoice Item')
    print('%s %s' % ('=' * 6, '=' * 70))

    if items is not None and items != []:
        field_width = ceil(log10(len(items)))

        for i, k in enumerate(items):
            print(' [%*d]%s %s' % (field_width, i, ' ' * (3 - field_width), k))

    for i, k in enumerate(args):
        print('  (%s)  %s' % (menu[i], k))

    print('%s %s' % ('-' * 6, '-' * 70))

    if prompt is not None:
        print('%s' % prompt)

    choice = None
    while True:
        c = input('Enter choice (or "None"): ')
        if c == 'None' or len(c) == 0:
            choice = (None, None)
            break
        else:
            try:
                if int(c) < len(items):
                    choice = (int(c), None)
                    break
            except ValueError:
                if c.upper() in menu:
                    choice = (None, next(i for i, k in enumerate(menu) if k == c.upper()))
                    break
        print('Invalid choice')
    return choice
Пример #6
0
def get_kv_pair(prompt='Key'):
    k = input('%s:' % prompt)
    if k == '':
        return {}
    v = input('Value: ')
    return {k: v}
Пример #7
0
def ifinput(prompt, default):
    g = input('%s [%s]: ' % (prompt, default))
    if len(g) == 0:
        g = default
    return g