Script and Tests

Values to Metric Prefixes Conversion Script

The values_to_metric_prefixes script converts values to quantities with metric prefixes.  

  • Enter the values_to_metric_prefixes script into the micro:bit Python Editor.
  • Set the project name to values_to_metric_prefixes, then click Save.
  • Click Send to micro:bit.
# values_to_metric_prefixes

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

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

while True:
    text = input("Enter value: ")
    value = float(text)
    prefix = input("Enter metric prefix: ")
    exponent = prefix_exponents[prefix]
    quantity = value / (10 ** exponent)
    unit = input("Enter unit: ")
    prefix_unit = prefix + unit
    print("Value:", quantity, prefix_unit)
    print()

 

Conversion Script Tests

This script converts decimal values to quantities and metric prefixes.  

  • Click Show serial.
  • Click to the right of the Enter Quantity prompt, type 1 and then press the Enter key.
  • Continue typing the values in the image below  to the right of each prompt, and press Enter after each one.

 

Experiment with Other Values

  • This time, use the values like 0.050 s.  Here are some you will see in upcoming lessons.  Use the expressions on the left, like 50 ms, 38 kHz and so on.  Substitute ohm for Ω and u for µ.  
    • 0.050 s
    • 38,000 Hz
    • 0.0011 A
    • 0.050 V
    • 1000 ohms
    • 0.000010 F