<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def luhn(base, num_only=False, allow_lower_case=False):
 """Return the Luhn check digit for the given string. Args: base(str):
 string for which to calculate the check digit num_only(bool):
 allow only digits in `base` (default: False) allow_lower_case(bool):
 allow lower case letters in `base` (default: False) Returns: int: Luhn check digit Raises: ValueError: given `base` contains an unallowed character """

    if num_only:
        alphabet = _ALPHABET[:10]
    else:
        alphabet = _ALPHABET
    if allow_lower_case:
        base = base.upper()
    try:
        pre_calc = (_PRE_CALC[alphabet.index(c)] for c in reversed(base))
        cum = 0
        parity = 1
        for elem in pre_calc:
            val, parity = elem[parity]
            cum += val
    except ValueError:
        pass    # fall through
    else:
        return 10 - cum % 10
    # unallowed character detected
    if num_only:
        msg = 'The string given must only contain digits.'
    elif allow_lower_case:
        msg = 'The string given must only contain digits and ascii letters.'
    else:
        msg = 'The string given must only contain digits and upper case ' \
              'ascii letters.'
    raise ValueError(msg)