title calculating the resistance of a n band resistor using the color codes description resistors resist the flow of electrical current each one has a value that tells how strongly it resists current flow this value s unit is the ohm often noted with the greek letter omega the colored bands on a resistor can tell you everything you need to know about its value and tolerance as long as you understand how to read them the order in which the colors are arranged is very important and each value of resistor has its own unique combination the color coding for resistors is an international standard that is defined in iec 60062 the number of bands present in a resistor varies from three to six these represent significant figures multiplier tolerance reliability and temperature coefficient each color used for a type of band has a value assigned to it it is read from left to right all resistors will have significant figures and multiplier bands in a three band resistor first two bands from the left represent significant figures and the third represents the multiplier band significant figures the number of significant figures band in a resistor can vary from two to three colors and values associated with significant figure bands black 0 brown 1 red 2 orange 3 yellow 4 green 5 blue 6 violet 7 grey 8 white 9 multiplier there will be one multiplier band in a resistor it is multiplied with the significant figures obtained from previous bands colors and values associated with multiplier band black 100 brown 101 red 102 orange 103 yellow 104 green 105 blue 106 violet 107 grey 108 white 109 gold 101 silver 102 note that multiplier bands use gold and silver which are not used for significant figure bands tolerance the tolerance band is not always present it can be seen in four band resistors and above this is a percentage by which the resistor value can vary colors and values associated with tolerance band brown 1 red 2 orange 0 05 yellow 0 02 green 0 5 blue 0 25 violet 0 1 grey 0 01 gold 5 silver 10 if no color is mentioned then by default tolerance is 20 note that tolerance band does not use black and white colors temperature coeffecient indicates the change in resistance of the component as a function of ambient temperature in terms of ppmk it is present in six band resistors colors and values associated with temperature coeffecient black 250 ppmk brown 100 ppmk red 50 ppmk orange 15 ppmk yellow 25 ppmk green 20 ppmk blue 10 ppmk violet 5 ppmk grey 1 ppmk note that temperature coeffecient band does not use white gold silver colors sources https www calculator netresistorcalculator html https learn parallax comsupportreferenceresistorcolorcodes https byjus comphysicsresistorcolourcodes function returns the digit associated with the color function takes a list containing colors as input and returns digits as string getsignificantdigits black blue 06 getsignificantdigits aqua blue traceback most recent call last valueerror aqua is not a valid color for significant figure bands function returns the multiplier value associated with the color function takes color as input and returns multiplier value getmultiplier gold 0 1 getmultiplier ivory traceback most recent call last valueerror ivory is not a valid color for multiplier band function returns the tolerance value associated with the color function takes color as input and returns tolerance value gettolerance green 0 5 gettolerance indigo traceback most recent call last valueerror indigo is not a valid color for tolerance band function returns the temperature coeffecient value associated with the color function takes color as input and returns temperature coeffecient value gettemperaturecoeffecient yellow 25 gettemperaturecoeffecient cyan traceback most recent call last valueerror cyan is not a valid color for temperature coeffecient band function returns the number of bands of a given type in a resistor with n bands function takes totalnumberofbands and typeofband as input and returns number of bands belonging to that type in the given resistor getbandtypecount3 significant 2 getbandtypecount2 significant traceback most recent call last valueerror 2 is not a valid number of bands getbandtypecount3 sign traceback most recent call last valueerror sign is not valid for a 3 band resistor getbandtypecount3 tolerance traceback most recent call last valueerror tolerance is not valid for a 3 band resistor getbandtypecount5 tempcoeffecient traceback most recent call last valueerror tempcoeffecient is not valid for a 5 band resistor function checks if the input provided is valid or not function takes numberofbands and colors as input and returns true if it is valid checkvalidity3 black blue orange true checkvalidity4 black blue orange traceback most recent call last valueerror expecting 4 colors provided 3 colors checkvalidity3 cyan red yellow traceback most recent call last valueerror cyan is not a valid color function calculates the total resistance of the resistor using the color codes function takes numberofbands colorcodelist as input and returns resistance calculateresistance3 black blue orange resistance 6000 20 calculateresistance4 orange green blue gold resistance 35000000 5 calculateresistance5 violet brown grey silver green resistance 7 18 0 5 calculateresistance6 red green blue yellow orange grey resistance 2560000 0 05 1 ppmk calculateresistance0 violet brown grey silver green traceback most recent call last valueerror invalid number of bands resistor bands must be 3 to 6 calculateresistance4 violet brown grey silver green traceback most recent call last valueerror expecting 4 colors provided 5 colors calculateresistance4 violet silver brown grey traceback most recent call last valueerror silver is not a valid color for significant figure bands calculateresistance4 violet blue lime grey traceback most recent call last valueerror lime is not a valid color function returns the digit associated with the color function takes a list containing colors as input and returns digits as string get_significant_digits black blue 06 get_significant_digits aqua blue traceback most recent call last valueerror aqua is not a valid color for significant figure bands function returns the multiplier value associated with the color function takes color as input and returns multiplier value get_multiplier gold 0 1 get_multiplier ivory traceback most recent call last valueerror ivory is not a valid color for multiplier band function returns the tolerance value associated with the color function takes color as input and returns tolerance value get_tolerance green 0 5 get_tolerance indigo traceback most recent call last valueerror indigo is not a valid color for tolerance band function returns the temperature coeffecient value associated with the color function takes color as input and returns temperature coeffecient value get_temperature_coeffecient yellow 25 get_temperature_coeffecient cyan traceback most recent call last valueerror cyan is not a valid color for temperature coeffecient band function returns the number of bands of a given type in a resistor with n bands function takes total_number_of_bands and type_of_band as input and returns number of bands belonging to that type in the given resistor get_band_type_count 3 significant 2 get_band_type_count 2 significant traceback most recent call last valueerror 2 is not a valid number of bands get_band_type_count 3 sign traceback most recent call last valueerror sign is not valid for a 3 band resistor get_band_type_count 3 tolerance traceback most recent call last valueerror tolerance is not valid for a 3 band resistor get_band_type_count 5 temp_coeffecient traceback most recent call last valueerror temp_coeffecient is not valid for a 5 band resistor function checks if the input provided is valid or not function takes number_of_bands and colors as input and returns true if it is valid check_validity 3 black blue orange true check_validity 4 black blue orange traceback most recent call last valueerror expecting 4 colors provided 3 colors check_validity 3 cyan red yellow traceback most recent call last valueerror cyan is not a valid color function calculates the total resistance of the resistor using the color codes function takes number_of_bands color_code_list as input and returns resistance calculate_resistance 3 black blue orange resistance 6000ω 20 calculate_resistance 4 orange green blue gold resistance 35000000ω 5 calculate_resistance 5 violet brown grey silver green resistance 7 18ω 0 5 calculate_resistance 6 red green blue yellow orange grey resistance 2560000ω 0 05 1 ppm k calculate_resistance 0 violet brown grey silver green traceback most recent call last valueerror invalid number of bands resistor bands must be 3 to 6 calculate_resistance 4 violet brown grey silver green traceback most recent call last valueerror expecting 4 colors provided 5 colors calculate_resistance 4 violet silver brown grey traceback most recent call last valueerror silver is not a valid color for significant figure bands calculate_resistance 4 violet blue lime grey traceback most recent call last valueerror lime is not a valid color
valid_colors: list = [
    "Black",
    "Brown",
    "Red",
    "Orange",
    "Yellow",
    "Green",
    "Blue",
    "Violet",
    "Grey",
    "White",
    "Gold",
    "Silver",
]

significant_figures_color_values: dict[str, int] = {
    "Black": 0,
    "Brown": 1,
    "Red": 2,
    "Orange": 3,
    "Yellow": 4,
    "Green": 5,
    "Blue": 6,
    "Violet": 7,
    "Grey": 8,
    "White": 9,
}

multiplier_color_values: dict[str, float] = {
    "Black": 10**0,
    "Brown": 10**1,
    "Red": 10**2,
    "Orange": 10**3,
    "Yellow": 10**4,
    "Green": 10**5,
    "Blue": 10**6,
    "Violet": 10**7,
    "Grey": 10**8,
    "White": 10**9,
    "Gold": 10**-1,
    "Silver": 10**-2,
}

tolerance_color_values: dict[str, float] = {
    "Brown": 1,
    "Red": 2,
    "Orange": 0.05,
    "Yellow": 0.02,
    "Green": 0.5,
    "Blue": 0.25,
    "Violet": 0.1,
    "Grey": 0.01,
    "Gold": 5,
    "Silver": 10,
}

temperature_coeffecient_color_values: dict[str, int] = {
    "Black": 250,
    "Brown": 100,
    "Red": 50,
    "Orange": 15,
    "Yellow": 25,
    "Green": 20,
    "Blue": 10,
    "Violet": 5,
    "Grey": 1,
}

band_types: dict[int, dict[str, int]] = {
    3: {"significant": 2, "multiplier": 1},
    4: {"significant": 2, "multiplier": 1, "tolerance": 1},
    5: {"significant": 3, "multiplier": 1, "tolerance": 1},
    6: {"significant": 3, "multiplier": 1, "tolerance": 1, "temp_coeffecient": 1},
}


def get_significant_digits(colors: list) -> str:
    
    digit = ""
    for color in colors:
        if color not in significant_figures_color_values:
            msg = f"{color} is not a valid color for significant figure bands"
            raise ValueError(msg)
        digit = digit + str(significant_figures_color_values[color])
    return str(digit)


def get_multiplier(color: str) -> float:
    
    if color not in multiplier_color_values:
        msg = f"{color} is not a valid color for multiplier band"
        raise ValueError(msg)
    return multiplier_color_values[color]


def get_tolerance(color: str) -> float:
    
    if color not in tolerance_color_values:
        msg = f"{color} is not a valid color for tolerance band"
        raise ValueError(msg)
    return tolerance_color_values[color]


def get_temperature_coeffecient(color: str) -> int:
    
    if color not in temperature_coeffecient_color_values:
        msg = f"{color} is not a valid color for temperature coeffecient band"
        raise ValueError(msg)
    return temperature_coeffecient_color_values[color]


def get_band_type_count(total_number_of_bands: int, type_of_band: str) -> int:
    
    if total_number_of_bands not in band_types:
        msg = f"{total_number_of_bands} is not a valid number of bands"
        raise ValueError(msg)
    if type_of_band not in band_types[total_number_of_bands]:
        msg = f"{type_of_band} is not valid for a {total_number_of_bands} band resistor"
        raise ValueError(msg)
    return band_types[total_number_of_bands][type_of_band]


def check_validity(number_of_bands: int, colors: list) -> bool:
    
    if number_of_bands >= 3 and number_of_bands <= 6:
        if number_of_bands == len(colors):
            for color in colors:
                if color not in valid_colors:
                    msg = f"{color} is not a valid color"
                    raise ValueError(msg)
            return True
        else:
            msg = f"Expecting {number_of_bands} colors, provided {len(colors)} colors"
            raise ValueError(msg)
    else:
        msg = "Invalid number of bands. Resistor bands must be 3 to 6"
        raise ValueError(msg)


def calculate_resistance(number_of_bands: int, color_code_list: list) -> dict:
    
    is_valid = check_validity(number_of_bands, color_code_list)
    if is_valid:
        number_of_significant_bands = get_band_type_count(
            number_of_bands, "significant"
        )
        significant_colors = color_code_list[:number_of_significant_bands]
        significant_digits = int(get_significant_digits(significant_colors))
        multiplier_color = color_code_list[number_of_significant_bands]
        multiplier = get_multiplier(multiplier_color)
        if number_of_bands == 3:
            tolerance_color = None
        else:
            tolerance_color = color_code_list[number_of_significant_bands + 1]
        tolerance = (
            20 if tolerance_color is None else get_tolerance(str(tolerance_color))
        )
        if number_of_bands != 6:
            temperature_coeffecient_color = None
        else:
            temperature_coeffecient_color = color_code_list[
                number_of_significant_bands + 2
            ]
        temperature_coeffecient = (
            0
            if temperature_coeffecient_color is None
            else get_temperature_coeffecient(str(temperature_coeffecient_color))
        )
        resisitance = significant_digits * multiplier
        if temperature_coeffecient == 0:
            answer = f"{resisitance}Ω ±{tolerance}% "
        else:
            answer = f"{resisitance}Ω ±{tolerance}% {temperature_coeffecient} ppm/K"
        return {"resistance": answer}
    else:
        raise ValueError("Input is invalid")


if __name__ == "__main__":
    import doctest

    doctest.testmod()