Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Electrical Measurements

Electrical Measurements

Prefix to Values Script and Tests

Metric Prefixes to Values Conversion Script

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

Example Script: metric_prefixes_to_values

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

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

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

while True:
    text = input("Enter quantity: ")
    quantity = float(text)
    
    prefix = input("Enter metric prefix: ")
    exponent = prefix_exponents[prefix]
    
    unit = input("Enter unit: ")

    value = quantity * (10 ** exponent)

    digits = str(abs(exponent) + 1)
    
    if value < 1:
        fstr = "%." + digits + "f"
    else:
        fstr = "%" + digits + ".0f"
        
    print("Value:", fstr %value, unit)
    
    print()

 

Conversion Script Tests

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

  • Click Show serial.
    (See Use the Serial Monitor.)
  • 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

Here are some you will see in upcoming lessons. 

  • Experiment with other values, using the expressions on the left, like 50 ms, 38 kHz and so on.  Substitute ohm for Ω and u for µ.  

 

  • t = 50 ms  —  time is 0.050 seconds
  • f = 38 kHz  —   frequency of 38,000 repetitions per second. Because of the micro:bit module’s 32-bit floating point math, you might get a value like 37999 instead.
  • I = 1.1 mA  —   current is 0.0011 amps
  • V = 5 mV  —  voltage is 0.0050 V
  • R = 1 kΩ   —  resistance is 1000 ohms
  • C = 10 µF —  capacitance is 0.000010 farads

 


Printer-friendly version
Convert Prefixes to Values
Prev
How the Prefix to Values Script Works
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress