How the Calculator Script Works
The script starts inside the while True loop, prompting you with "Enter volts: ". The characters you type are stored in the text variable. Then V = float(text) converts the characters you have typed from text into a floating point value and stores the result in a variable named V. It repeats those steps for loading the ohms value you enter into the variable R.
while True: text = input("Enter volts: ") V = float(text) text = input("Enter ohms: ") R = float(text)
Since V and R are known, the I = V / R form of the Ohm’s Law equation calculates the current value I.
I = V / R
After printing "I = ", the value of I, and the “A” unit, the script prints an empty line. After that, the while True loop repeats so that you can calculate another current value.
print("I = ", I, "A") print()
Try This: Calculate V from A and Ω
Try modifying your script to calculate volts from amps and ohms.
- Modify your script as shown below.
- Update the name in the project name field to calculate_v_from_i_and_r. Then , click Load/Save → Download Project Hex to save a copy of your work.
- Click Send to micro:bit.
- Try entering some of the measurements you have taken previously into the serial monitor.
- Verify that they match the calculated values.