TactivergeLite

Lite / Building Machines That Respond / Term 1 worksheet

Worksheet: Building Machines That Respond, term 1

Computational Thinking · Questions only. Check your answers in each Lite lesson, where they are graded on our server.

Name:   Date:

Week 1: Break a machine's job into steps

1. Which step is too vague for a computer?

2. Why do engineers decompose a big job before coding it?

3. A step says 'Open the valve until moisture reaches 45 %'. What is the safest improvement?

4. For the watering algorithm, which is an output?

5. Why must 'Close the water valve' come after 'Keep the valve open until …'?

Check these in the lesson

Week 1: Flowcharts: drawing decisions and loops

1. A tank starts with 55 L of water. The controller runs: while level < 100: level = level + 10 (one pump pass adds 10 L). How many times does the pump run before the loop stops?

Answer: passes

2. In a flowchart, what does a diamond mean?

3. A loop runs while level != 100 and adds 30 L per pass, starting at 0 L. What happens?

4. Start at 10 L, add 20 L per pass, loop while level < 75. How many passes does the pump make?

Answer: passes

5. Why add 'or 20 passes have run' to the tank loop?

Check these in the lesson

Week 1: Trace every case with a rule table

1. A sorting machine's decision depends on 4 yes/no sensors and one selector switch with 2 positions. How many different input combinations must a complete test table contain?

Answer: rows

2. A rule table for 3 yes/no sensors has only 7 rows. What is the risk?

3. Which test input is most likely to expose a gap in a sorting algorithm?

4. A machine has 4 yes/no sensors. How many rows does a complete rule table need?

Answer: rows

Check these in the lesson

Week 2: Your first Python program: battery runtime

1. A sensor node runs from a 2500 mAh battery and draws a steady 50 mA. A program computes hours = 2500 / 50 and then minutes = hours * 60. What value does minutes hold, in minutes? (Ignore the battery ageing or the voltage dropping.)

Answer: min

2. What does print("3 + 4") show?

3. What does Python do with # battery capacity in mAh?

4. Using the same formula, a 3000 mAh battery powers a 25 mA device. How many days is that?

Answer: days

5. Which variable name is best for a current measured in milliamps?

Check these in the lesson

Week 2: How the interpreter reads your program

1. Trace this Python by hand: x = 8; x = x + 1; x = x * 4; x = x - 20; print(x). What number is printed?

Answer:

2. a = 5; b = a * 2; a = 100; print(b). What is printed?

3. Which program raises a NameError?

4. In Python, what does = mean in total = total + 5?

Check these in the lesson

Week 3: Variables that track a changing machine

1. A robot starts with distance_m = 0.0 and its loop runs distance_m = distance_m + speed_m_per_s * dt_s exactly 7 times, with speed_m_per_s = 2 and dt_s = 0.1. What is distance_m at the end, in metres?

Answer: m

2. Which lines correctly swap a and b?

3. Which is a valid Python variable name?

4. count = 0, then count += 1 runs 3 times, then count = count * 2 runs once. What is count?

Answer:

5. A program runs total_wh = total_wh + 5 but never set total_wh before. What happens?

Check these in the lesson

Week 3: int, float, str and list

1. A machine makes 26 parts and packs them in boxes of 12. What does Python give for 26 // 12 (full boxes)?

Answer: parts

2. What is the type of 7 // 2?

3. reading = "23.5". Which line gives the number 23.9?

4. What does "7" + "2" give?

5. A machine makes 250 parts and packs 12 per box. How many parts are left over (250 % 12)?

Answer: parts

Check these in the lesson

Week 3: Why 0.1 + 0.2 is not 0.3

1. Which test is the right way to check that a computed voltage v is 3.3 V?

2. Which of these decimals is stored exactly as a Python float?

3. A regulator should output 2.5 V. A test reads 2.502 V. What is the absolute error, |measured − expected|, in millivolts?

Answer: mV

4. What does print(0.1 * 3 == 0.3) show?

5. A timer adds 0.1 s every tick. What is the most reliable design?

Check these in the lesson

Week 4: Translate a formula into Python

1. Which Python line correctly computes V_out = V_in·R2/(R1 + R2)?

2. What does Python give for -3 ** 2?

3. A 2 kg robot moves at 36 km/h. Convert to m/s (divide by 3.6), then use E = 0.5 * m * v ** 2. What is its kinetic energy, in joules?

Answer: J

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

5. A robot drives 300 m in 100 s, then 100 m in 100 s. What is its average speed, in m/s?

Answer: m/s

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

Check these in the lesson

Week 4: Rearrange the formula, then compute: an LED resistor

1. An LED with a forward voltage of 1.8 V should carry 15 mA from a 9 V supply. Using r_ohm = (v_supply - v_forward) / (current_ma / 1000), what resistance is needed, in ohms?

Answer: Ω

2. Rearrange V = I · R to find I.

3. A program does r = (5 - 2) / 20 with the current in mA. What goes wrong?

4. A resistor drops 4.5 V at 0.030 A. What power does it dissipate, in watts?

Answer: W

Check these in the lesson

Week 4: The math module: roots, angles and radians

1. A loading ramp is 2.7 m long and rises at 5° to the floor. Python computes height_m = 2.7 * math.sin(math.radians(5)). What is the height, in metres?

Answer: m

2. Which line correctly computes cos of 60°?

3. A rectangular frame is 80 cm wide and 115 cm tall. Python computes brace_cm = math.sqrt(80 ** 2 + 115 ** 2). How long is the diagonal brace, in centimetres?

Answer: cm

4. Why is math.sqrt(w ** 2) + math.sqrt(h ** 2) not the brace length?

5. What is 45° in radians, to 3 decimal places?

Answer: rad

Check these in the lesson

Week 5: Decisions from measurements: if, elif, else

1. A cooling fan is set by: if temp_c >= 45: fan = 100, elif temp_c >= 35: fan = 50, else: fan = 0. What is fan, in %, when temp_c = 44.5?

Answer: %

2. A rule is written: if temp_c >= 35: fan = 50, elif temp_c >= 50: fan = 100. What is wrong?

3. Which condition tests whether a reading equals the limit?

4. When does the else block in charge_mode run?

Check these in the lesson

Week 5: and, or, not: safety interlocks in code

1. guard, operator and fault are each True or False (8 combinations). For how many of the 8 combinations is this Python condition True: guard or operator?

Answer: rows

2. Which condition is True exactly when temp_c is from 0 to 45 inclusive?

3. Why is (guard or operator) and not fault a dangerous interlock?

4. How does Python read not fault and guard or override?

5. Why does count > 0 and total / count > 50 never divide by zero?

Check these in the lesson

Week 5: Test the edges: boundary values

1. A thermometer reports in steps of 0.5 °C. The alarm rule is if temp_c >= 30: alarm(). As the temperature rises from 28 °C one step at a time, what is the first reading that sets off the alarm, in °C?

Answer: °C

2. A rule should pass readings from 10 to 60 inclusive, in steps of 1. Which set of tests is best?

3. Where should a test's expected value come from?

4. Version A uses if temp_c <= 0: return 'BLOCK: too cold'. Which input reveals its bug?

5. A rule has three thresholds. Using on, just-below and just-above tests for each, how many boundary tests is that?

Answer: tests

Check these in the lesson

Week 6: for loops: summarise a sensor log

1. A loop totals and counts these temperature readings (°C): [18.0, 25.4, 31.8, 31.6, 21.9, 26.6]. What is total / count, in °C?

Answer: °C

2. How many times does the body of for r in readings: run if readings has 7 items?

3. What happens with total / len(readings) when readings is []?

4. In a loop that totals readings, where should print(total) go to print the total once?

5. Readings 20, 22, 24 and 26 °C. What is the mean, in °C?

Answer: °C

Check these in the lesson

Week 6: Count, flag and find extremes

1. A loop runs if r > 55: over = over + 1 for each reading r in [62.7, 51.5, 58.6, 63.0, 54.8, 55.0] (°C), starting from over = 0. What is over at the end?

Answer: readings

2. To find the hottest reading in a loop, what should hottest start as?

3. Why report the max, the min and a glitch count as well as the mean?

4. Readings 18.2, 25.6, 19.9 and 23.4 °C. What is the range, max − min, in °C?

Answer: °C

Check these in the lesson

Week 6: while loops: repeat until it's done

1. A motor is 75 °C hotter than the room. Each minute of cooling multiplies the difference by 0.5. The program runs: while excess > 5: excess = excess * 0.5; minutes = minutes + 1 (starting from excess = 75, minutes = 0). What is minutes when the loop ends?

Answer: min

2. Which while loop never ends?

3. Which job suits a while loop best?

4. x = 2. How many times does while x > 5: x = x - 1 run?

Check these in the lesson

Week 7: Package a calculation as a function

1. def wheel_speed_m_per_s(rpm, diameter_m): return rpm / 60 * math.pi * diameter_m. What does wheel_speed_m_per_s(150, 0.05) return, in m/s?

Answer: m/s

2. Why is return better than print inside a calculation function?

3. def area_m2(width_m, height_m): return width_m * height_m. What does area_m2(3, 0.5) return?

4. def f(x): x * 2 (no return). What does f(4) give?

5. Using v = rpm / 60 × π × d, what speed in m/s does a 0.2 m wheel at 30 rpm give, to 3 decimal places?

Answer: m/s

Check these in the lesson

Week 7: Promises and proof: tests with assert

1. You are testing def current_ma(v, r_ohm): return v / r_ohm * 1000. To write assert abs(current_ma(9, 100) - EXPECTED) < 0.01, work EXPECTED out by hand with Ohm's law. What is it, in mA?

Answer: mA

2. A correct function returns 129.99999999999997 for an expected 130. Which test is right?

3. Why shouldn't you run the function and paste its output into the test as the expected value?

4. Which set of inputs best tests c_to_f?

Check these in the lesson

Week 7: Decompose a program into functions

1. A 10-bit ADC reads raw = 307. to_volts(raw) returns raw * 3.3 / 1023, and to_celsius(v) returns (v - 0.5) * 100 (a TMP36-style sensor: 10 mV per °C with a 0.5 V offset). What is to_celsius(to_volts(307)), in °C?

Answer: °C

2. Which function does one job?

3. Why test to_volts and to_celsius separately before the whole pipeline?

4. With volts = raw × 3.3 / 1023, how many millivolts is one ADC count, to 2 decimal places?

Answer: mV

Check these in the lesson

Week 8: Read the traceback

1. Which part of a traceback tells you the kind of error?

2. Which is a logic error?

3. float("ERR") raises which error?

4. A list has 7 readings. What is the largest valid index?

Answer:

Check these in the lesson

Week 8: Reproduce, hypothesise, test: hunting logic bugs

1. readings = [15.0, 22.9, 29.8, 29.6, 19.2]. The buggy code sets total = 0, runs for i in range(1, len(readings)): total = total + readings[i], then calls print(total / len(readings)). What does it print (to 2 decimal places)?

Answer:

2. A program gives a wrong total. What is the best first step?

3. A function returns after checking only the first reading. What is the most likely cause?

4. distance_mm differences divided by interval_s are printed as m/s and look 1000 times too big. Best fix?

Check these in the lesson

Week 8: Catch bad data before it bites

1. A log has the lines ["-999.0","25.8","-999.0","21.4","21.6","19.1","20.0","--"]. The program keeps a line only if float(line) works and -40 <= value <= 125 (the sensor's rated range, °C); otherwise it skips it. How many readings are kept?

Answer: readings

2. Which check rejects the line "130.5" from a sensor rated −40 to 125 °C?

3. Why is replacing bad lines with 0 a bad idea?

4. Why write except ValueError rather than a bare except?

Check these in the lesson

Week 9: Plan the sensor-log checker

1. The safe band is 15–45 °C inclusive. Readings (°C): 44, 33, 28, 29, 20, 23, 49, 49, 24, 12, 33, 24. What percentage of the readings are outside the band?

Answer: %

2. Which test log best checks the safe-band boundary?

3. What should the checker do with an empty log?

4. Why write each function's docstring before its body?

Check these in the lesson

Week 9: Build it, test it, break it

1. A motor log has the lines ["40.0","","50.6","n/a","","59.7","59.4","45.6","52.3"] (°C). The checker skips any line that float() cannot convert and averages the rest. What mean does it report, in °C?

Answer: °C

2. In what order should you build the checker?

3. decide([10, 60], 0) returns INSPECT. Which line is most likely wrong?

4. Which is the best bug-diary entry?

Check these in the lesson