TactivergeLite

Lite / Building Machines That Respond / Week 4

Translate a formula into Python

Two programs compute a voltage divider's output. One says 3.4 V, the other 2211 V. Which pair of brackets is missing?

About 40 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

The maths in a textbook uses layout to show order: a fraction bar, a raised power. Code is one flat line, so the order has to be spelled out with precedence and brackets. This is where engineering mathematics meets programming, and where many real calculation bugs start.

Predict

With v_in = 5, r1 = 1000 and r2 = 2200, what does v_in * r2 / r1 + r2 give?

  1. 3.4375
  2. 2211.0
  3. An error
Guess first, then reveal

2211.0. Division happens before addition, so Python computes (5 × 2200 / 1000) + 2200 = 11.0 + 2200. The formula needs v_in * r2 / (r1 + r2) = 3.4375 V.

Discover

Pick 'Voltage divider' and step through each expression: in which step does the wrong one go astray? Then try 'Kinetic energy' (is 1 / 2 * m * v ** 2 right?) and 'Average speed' (why is averaging the two speeds wrong?).

What the interactive shows (text version)

The precedence explorer shows a formula, sliders for its inputs, and several Python expressions. For the chosen expression it lists every evaluation step. Example, voltage divider with 5 V, 1000 Ω, 2200 Ω: v_in * r2 / (r1 + r2) → 5 * 2200 / 3200 → 11000 / 3200 → 3.4375 (matches); v_in * r2 / r1 + r2 → 11000 / 1000 + 2200 → 11.0 + 2200 → 2211.0 (does not match).

You can also open the interactive on the full site when you have a faster connection.

Understand

Python's order of operations

  1. Brackets. 2. Powers ** (worked right to left). 3. Unary minus, so -3 ** 2 is -9. 4. *, /, //, % from left to right. 5. +, - from left to right. When in doubt, add brackets: they cost nothing and show your intent.

A translation recipe

  1. Name each quantity with its unit: mass_kg, speed_m_per_s. 2. Replace a fraction bar with / and wrap the whole top and bottom in brackets. 3. Write powers with **. 4. Run it with numbers you can check by hand.
mass_kg = 1.2
speed_m_per_s = 3
energy_j = 0.5 * mass_kg * speed_m_per_s ** 2
print(round(energy_j, 2))   # 5.4

Units check and estimate

For E = ½mv²: kg × (m/s)² = kg·m²/s² = J, so the formula is consistent. Estimate: ½ × 1 × 3² ≈ 4.5, close to 5.4. If an answer is out by a factor of 1000 or has the wrong unit, a conversion or bracket is missing.

Explain it another way

Intuitive

A formula on paper uses its shape (a fraction bar, a small raised 2) to show what happens first. Code has no shape, so brackets do that job.

Technical

Python parses an expression into a tree using precedence and associativity, then evaluates it depth-first, left operand before right. is right-associative: 2 3 2 = 2 9 = 512.

Mathematical

V_out = V_in·R₂/(R₁ + R₂). With 5 V, 1 kΩ and 2.2 kΩ: 5 × 2200 / 3200 = 3.4375 V. Check the limits: if R₂ → 0, V_out → 0; if R₁ → 0, V_out → V_in. Both are physically right.

Applied

Voltage dividers scale a battery voltage down so a microcontroller can measure it. A bracket slip in the scaling code makes the robot think its battery is full when it is nearly flat.

Derivation: Average speed is total distance over total time
  1. A robot drives d₁ = 120 m in t₁ = 40 s, then d₂ = 80 m in t₂ = 60 s.

  2. Average speed means one steady speed that covers the same total distance in the same total time: v = (d₁ + d₂) / (t₁ + t₂).

  3. Substitute: v = (120 + 80) / (40 + 60) = 200 m / 100 s = 2.0 m/s.

  4. Averaging the two speeds gives (3.0 + 1.33) / 2 ≈ 2.17 m/s, which is wrong because the robot spends longer on the slow leg.

  5. In Python the brackets carry the fraction bar: (d1_m + d2_m) / (t1_s + t2_s). Without them, d1_m + d2_m / t1_s + t2_s gives 120 + 2 + 60 = 182.0, with meaningless units.

Worked example 1

Write F = C × 9/5 + 32 in Python and check it at 100 °C.

temp_f = temp_c * 9 / 5 + 32. Left to right: 100 × 9 = 900, / 5 = 180.0, + 32 = 212.0 °F, which is right for boiling water. temp_c * 9 / (5 + 32) would divide by 37 instead and give about 24.3.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. Which Python line correctly computes V_out = V_in·R2/(R1 + R2)?
Practice 2. What does Python give for -3 ** 2?

J

Practice 4. Is 1 / 2 * m * v ** 2 a correct translation of ½mv² in Python 3?

m/s

Practice 6. Someone writes power_w = voltage_v / current_a. What does a units check say?

Apply it

Translate three formulas you know (for example density = mass/volume, P = V·I, and the voltage divider) into Python with named variables and units. For each, write one test value you can check by hand and run it.

Reflect

Which precedence rule surprised you most, and how will you protect yourself from it?

Write your answer on paper or in your own notes. Lite never stores what you write.

Go further

← Why 0.1 + 0.2 is not 0.3Rearrange the formula, then compute: an LED resistor →