(vec,
                        msg,
                        label,
                        to_str,
                        help_msg=None,
                        none_option=False,
                        none_str="None of the above")
Multiple choice question

    Args:
        vec         : Vector with elements to choose from
        msg         : Msg to prompt user for choice
        label       : Description/label of what "vec" holdes
        to_str      : Function that prints information about elements
        help_msg    : Optional help message
        none_option : Whether to display a "none of the above" option
        none_str    : What do display as "none of the above" message

    Returns:
        Chosen element
Multiple choice question
Multiple choice question
def ask_multiple_choice(vec,
                        msg,
                        label,
                        to_str,
                        help_msg=None,
                        none_option=False,
                        none_str="None of the above"):
    """Multiple choice question

    Args:
        vec         : Vector with elements to choose from
        msg         : Msg to prompt user for choice
        label       : Description/label of what "vec" holdes
        to_str      : Function that prints information about elements
        help_msg    : Optional help message
        none_option : Whether to display a "none of the above" option
        none_str    : What do display as "none of the above" message

    Returns:
        Chosen element

    """
    if (none_option):
        assert (len(vec) > 0)
    else:
        assert (len(vec) > 1)

    print(msg)

    for i_elem, elem in enumerate(vec):
        elem_str = to_str(elem)
        print("[%2u] %s" % (i_elem, elem_str))

    if (none_option):
        print("[%2u] %s" % (len(vec), none_str))

    if (help_msg is not None):
        print()
        print(help_msg)

    resp = None
    while (not isinstance(resp, int)):
        try:
            resp = int(_input("\n%s number: " % (label)))
        except ValueError:
            print("Please choose a number")
            continue

        max_resp = len(vec) + 1 if none_option else len(vec)
        if (resp >= max_resp):
            print("Please choose number from 0 to %u" % (max_resp - 1))
            resp = None
            continue

        if (none_option and resp == len(vec)):
            choice = None
            print(none_str)
        else:
            choice = copy.deepcopy(vec[resp])
            # NOTE: deep copy to prevent the caller from changing the original
            # element.
            print(to_str(choice))
        print()

        return choice
def ask_multiple_choice ( vec , msg , label , to_str , help_msg = None , none_option = False , none_str = "None of the above" ) : if ( none_option ) : assert ( len ( vec ) > 0 ) else : assert ( len ( vec ) > 1 ) print ( msg ) for i_elem , elem in enumerate ( vec ) : elem_str = to_str ( elem ) print ( "[%2u] %s" % ( i_elem , elem_str ) ) if ( none_option ) : print ( "[%2u] %s" % ( len ( vec ) , none_str ) ) if ( help_msg is not None ) : print ( ) print ( help_msg ) resp = None while ( not isinstance ( resp , int ) ) : try : resp = int ( _input ( "\n%s number: " % ( label ) ) ) except ValueError : print ( "Please choose a number" ) continue max_resp = len ( vec ) + 1 if none_option else len ( vec ) if ( resp >= max_resp ) : print ( "Please choose number from 0 to %u" % ( max_resp - 1 ) ) resp = None continue if ( none_option and resp == len ( vec ) ) : choice = None print ( none_str ) else : choice = copy . deepcopy ( vec [ resp ] ) # NOTE: deep copy to prevent the caller from changing the original # element. print ( to_str ( choice ) ) print ( ) return choice