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)
ColourAbbreviationSignificant figuresMultiplierTolerance
Blackbk01--
Brownbr110$\pm 1\%$
Redrd2$10^2$$\pm 2\%$
Orangeor3$10^3$--
Yellowyl4$10^4$$\pm 5\%$
Greengr5$10^5$$\pm 0.5\%$
Bluebl6$10^6$$\pm 0.25\%$
Violetvi7$10^7$$\pm 0.1\%$
Greygy8$10^8$$\pm 0.05\%$
Whitewh9$10^9$--
Goldau----$\pm 5\%$
Silverag----$\pm 10\%$
None------$\pm 20\%$