How the Prefix to Values Script Works

How the metric_prefixes_to_values Script Works

The metric_prefixes_to_values script converts quantities and metric prefixes to the numbers they represent.  

This script's first statement is called a dictionary.  A dictionary has a name. (prefix_exponents) and key-value pairs (Like 'M':6 and 'k':3).  The keys are 'M’, 'k', 'm', and 'u'.  The value paired with 'M' is 6; the value paired with 'k' is 3, and so on…  Later in the script, a statement will retrieve the value that’s paired with a given key and use it as an exponent.  For example 3, will become 1 x 103.  To learn more about dictionaries, try the Dictionary Primer.  

prefix_exponents = {'M':6, 'k':3, 'm':-3, 'u':-6}

 

These are print statements.  Each print statement contains a string object, the text between quotes.  The one that’s just print() adds an empty line before the next print statement.

print("Enter quantity, metric prefix, and unit.")
print("Result will be a decimal value.")
print()

 

Inside the endless while True loop, text = input(“Enter quantity: “) displays Enter quantity: and then stores the value you type in a variable named textquantity = float(text) converts the characters you typed into a floating-point number that the micro:bit can use in statements that make calculations.

while True:
    text = input("Enter quantity: ")
    quantity = float(text)  

 

Another input statement stores the metric prefix you type (like M, k, m, or u) in a variable named prefix.  Then, prefix_exponents[prefix] looks up the value that’s paired with that prefix in the dictionary at the beginning of the script.  For example, if you typed M, the key is 'M', so the value that gets returned by prefix_multipliers[prefix] will be 6, and that’s the value that gets stored in the exponent variable.  Another example, If you typed k, the key will be 'k', and the value that gets stored in exponent will be 3.  Again, to learn more, try the Dictionary Primer

    prefix = input("Enter metric prefix: ")
    exponent = prefix_exponents[prefix]

 

This final input statement just stores a unit you type.  Those should be SI units like A, F, Hz, and V.  For Ω, substitute ohm.

    unit = input("Enter unit: ")

 

The quantity and exponent variables were set earlier.  Quantity is the number you typed in response to the "Enter quantity" prompt like 1 or 20.  The exponent variable stores the value from the dictionary.  So, when you typed k, it looked up 3 in the dictionary, or when you typed m, it looked up -3.  So, if you typed a quantity of 1 and a prefix of k, this would multiply 1 by 1x103 for a result of 1000.  If you typed a quantity of 20 and a prefix of m, it would multiply 20 by 1 x 10-3  for a result of 0.02, and so on…  

    value = quantity * (10 ** exponent)

 

The script will need a string with a character in it, like ‘4’ for k or m, or ‘7’ for m or u, to help format the value it displays.  It starts by taking the absolute value of the exponent.  So, regardless of whether the exponent is -3 or 3, the absolute value will be 3.  Then, it adds 1 to that value.  Then, the value (7 or 4) is converted to a string that contains ‘7’ or ‘4’.  The resulting string with the character is stored in a variable named digits.

    digits = str(abs(exponent) + 1)

 

Don’t worry too much about the details of this part; it’s a little more advanced.  Scripts can use statements like print("value %3.2f", value) to display a total of 3 digits with 2 to the right of the decimal point.  These lines in the script build a formatting string like “%3.2f”, but adjusted to the size of the value the script is about to display.  

   if value < 1:
        fstr = "%." + digits + "f"
    else:
        fstr = "%" + digits + ".0f"

 

This statement makes the result appear in the Serial Terminal.  It prints "Value: " followed by the specially formatted string that represents the value, followed by the unit. For example, this is what makes 1000 ohm or 0.0200 s appear.

    print("Value:", fstr %value, unit)

 

This prints an empty line before the while True loop repeats and asks you to enter your next quantity.

    print()

Try This: Expand the Dictionary

If you use metric prefixes that are not in the dictionary, such as G, n or p, the script will display a message about an exception.  

  • Replace the dictionary at the beginning of the script with this one.  
prefix_exponents = {'G':9, 'M':6, 'k':3,
                    'm':-3, 'u':-6, 'n':-9, 'p':-12}
  • Rename the script to metric_prefixes_to_values_try_this.
  • Test values with G, n, and p.

Again, because of how the micro:bit module’s 32-bit floating point arithmetic works, the results will be slightly different from what you get with a calculator.  It’ll be close, but not quite as precise.  For example, if you expect 40000000000, you might get something like 39999995231.  The result is still more than 99.9999% correct.