Learning Scientific Programming with Python (2nd edition)
P4.2.1: Resistor colour codes
Question P4.2.1
The values and tolerances of older resistors are identified by four coloured bands: the first two indicate the first two significant figures of the resistance in ohms, the third denotes a decimal multiplier (number of zeros), and the fourth indicates the tolerance. The colours and their meanings for each band are listed in the table below.
For example, a resistor with coloured bands: violet, yellow, red, green has value $74 \times 10^2 = 7400\;\mathrm{\Omega}$ and tolerance $\pm 0.5\%$.
Write a program which defines a function to translate a list of four colour abbreviations into a resistance value and a tolerance. For example:
>>> print(get_resistor_value(['vi', 'yl', 'rd', 'gr']))
(7400, 0.5)
Colour | Abbreviation | Significant figures | Multiplier | Tolerance |
---|---|---|---|---|
Black | bk | 0 | 1 | -- |
Brown | br | 1 | 10 | $\pm 1\%$ |
Red | rd | 2 | $10^2$ | $\pm 2\%$ |
Orange | or | 3 | $10^3$ | -- |
Yellow | yl | 4 | $10^4$ | $\pm 5\%$ |
Green | gr | 5 | $10^5$ | $\pm 0.5\%$ |
Blue | bl | 6 | $10^6$ | $\pm 0.25\%$ |
Violet | vi | 7 | $10^7$ | $\pm 0.1\%$ |
Grey | gy | 8 | $10^8$ | $\pm 0.05\%$ |
White | wh | 9 | $10^9$ | -- |
Gold | au | -- | -- | $\pm 5\%$ |
Silver | ag | -- | -- | $\pm 10\%$ |
None | -- | -- | -- | $\pm 20\%$ |