Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
1.8 kB
5
Indexable
Never
bandVal = {
    '0': "black",
    '1': "brown",
    '2': "red",
    '3': "orange",
    '4': "yellow",
    '5': "green",
    '6': "blue",
    '7': "violet",
    '8': "grey",
    '9': "white",
}

bandMul = {
    1    : "black",
    10   : "brown",
    100  : "red",
    1000 : "orange",
    10**4: "yellow",
    10**5: "green",
    10**6: "blue",
    10**7: "violet",
    10**8: "grey",
    10**9: "white",
    0.1  : "gold",
    0.01 : "silver",
}

bandTol = {
    '+-1%'   : "brown",
    '+-2%'   : "red",
    '+-0.5%' : "green",
    '+-0.25%': "blue",
    '+-0.1%' : "violet",
    '+-5%'   : "gold",
    '+-10%'  : "silver",
    '+-20%'  : "none",
}

#-----------------------------------------------

def colorOfResistor(val) :
    band12Key = '10'
    band3Key = 1
    band4Key = '+-5%'

    resColor = []

    resVal = float(val) # input

    if resVal < 0.1 :
        print('?')
    elif 0.1 <= resVal < 1.0 :
        band12Key = str(int(resVal * 100))
        band3Key = 0.01
    elif resVal < 10.0 : # 1.0 <= resVal < 10.0 :
        band12Key = str(int(resVal * 10))
        band3Key = 0.1
    elif resVal < 99.6 : # 10.0 <= resVal < 99.6 :
        resValRound = int(round(resVal, 0))
        band12Key = str(resValRound)[0:2]
        band3Key = 1
    else : # 99.6 <= resVal :
        resValRound = int(round(resVal, 0)) # 129.3 -> 129.0
        band3Key = 10 ** (len(str(resValRound)) - 2)
        resValRound = int(round(resVal / band3Key)) # 129.0 -> 13
        band12Key = str(resValRound)[0:2]

    resColor = [ bandVal[band12Key[0]], bandVal[band12Key[1]], bandMul[band3Key], bandTol[band4Key] ]
    return resColor

#-----------------------------------------------

res = input("Enter the resistance value: ")

print(res, 'ohm =', colorOfResistor(res))