vendredi 11 juin 2021

how to use if-else from a function into another function

I have a function 'get_number(code, attribute)' that outputs a specific section of a code that is associated with a specific piece of information. But I'm not sure how I would use this function within 'print_description(code)' so I can get the specific section of the code when the function only has one return.

Under the 'print_description(code)' docstring, I want to replace those equations 'get_number(code, attribute)'

def get_number(code, attribute):
    """
    -------------------------------------------------------
    Returns number from code and attribute.
    Use: code = get_number(code, attribute)
    -------------------------------------------------------
    Parameters:
        code- an arbitrary code (str)
        attribute - some string representing a code attribute (str)
    Returns:
        number - value derived from attribute and serial number (int)
    -------------------------------------------------------
    """

    if attribute == 'x':
        number = (int(code)) // 100000
    elif attribute == 'y':
        number = ((int(code)) // 10000) % 10
    elif attribute == 'z':
        number = ((int(code))// 1000) % 10
    elif attribute == 'info':
        number = ((int(code)) % 1000)
    else:
        number = -99
    return number 

def print_description(code):
    """
    -------------------------------------------------------
    Print detailed description based on the code.
    Use: description = print_description(code)
    -------------------------------------------------------
    Parameters:
        code - an arbitrary serial number (str)
    Returns:
        x- x code (int)
        y- y code (int)
        z- z code (int)
        info - info code (int)
    -------------------------------------------------------
    """
    x_code = (int(code)) // 100000
    y_code = ((int(code)) // 10000) % 10
    z_code = ((int(code))// 1000) % 10
    info_num = ((int(code)) % 1000)
     
    if not isinstance(code, str):
        print('Invalid')
    else:
        print('{} {} {} {}'.format(x1, y1, info_num, z1))
    return

Aucun commentaire:

Enregistrer un commentaire