AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Helpers that sense

Week 29 · 4.5 Augmented reality and AI · 3 lessons
3

Project: shelter light

Computing 8: Topic 4.5, pp. 283–299
GoalPlan, make, test and evaluate an autonomous program for a bus shelter lamp.
New words
test plan · a table of tests with expected outcomes worked out before runningevaluate · say how well the solution works and how it could be betterinvalid data · data that should not be accepted, such as a sensor reading below 0
Explanation

Invented problem: a town wants a bus shelter lamp that turns on by itself, but only when it is dark and at least one person is waiting, to save power. Step 1: plan. List the inputs (a light sensor and a people counter), the decision and the output. Step 2: make. Write the program on paper. Step 3: test. Write a test plan with normal, boundary and invalid data, and work out each expected outcome before you run anything. Step 4: evaluate. Say what passed, what failed and how you would improve it. Then improve the program and test again.

PartPlan
Inputslight level 0 to 100; number of people waiting
Decisionlight below 30 AND people above 0
Outputlamp on or off
Worked examples
Python
1light = int(input("Light level 0 to 100? "))
2people = int(input("People waiting? "))
3if light < 30 and people > 0:
4    print("Shelter lamp on")
5else:
6    print("Shelter lamp off")
Output
Shelter lamp on
Make: the first program. The lamp comes on when the light is below 30 and someone is waiting. With 20 and 2 typed the output is Shelter lamp on.
TestType of dataTest dataExpected outcomeActual outcomePass/fail
1normal20, 2Shelter lamp onShelter lamp onpass
2normal80, 2Shelter lamp offShelter lamp offpass
3normal20, 0Shelter lamp offShelter lamp offpass
4boundary29, 1Shelter lamp onShelter lamp onpass
5boundary30, 1Shelter lamp offShelter lamp offpass
6invalid-5, 1Sensor errorShelter lamp onFAIL
Test: the test plan. Expected outcomes were worked out first. Five tests pass. The invalid test (a sensor reading of −5) was expected to give a Sensor error, but the program says the lamp is on, because −5 < 30. That is a test failure.
Python
1light = int(input("Light level 0 to 100? "))
2people = int(input("People waiting? "))
3if light < 0 or light > 100:
4    print("Sensor error")
5elif light < 30 and people > 0:
6    print("Shelter lamp on")
7else:
8    print("Shelter lamp off")
Output
Sensor error
Evaluate and improve: the lamp program works for sensible readings but does not check the sensor. The new version adds an invalid range check first, so a reading of -5 gives Sensor error. All six tests now pass. An even better version could tell the town to repair a faulty sensor.
Class activity

Your own project (on paper). Invented problem: a greenhouse window should open by itself when it is hot AND the wind is calm, so the plants do not get damaged. Plan: write the inputs, the decision and the output in a table. Make: write the program in Python or pseudocode, using a temperature of 0 to 50 and a wind speed from 0 to 100. Test: write a test plan with at least two normal tests, two boundary tests and one invalid test, with expected outcomes worked out before running. Evaluate: write three sentences about what failed and how to improve it. If you have a computer or tablet, ask an adult to help you run your program.

Practice
1
Write the expected outcome for these tests of the first shelter program: 10 and 3; 50 and 4; 29 and 0.
Light, peopleExpected outcome
10, 3?
50, 4?
29, 0?
2
A boundary test of 30 light and 1 person is expected to turn the lamp on. Is the expected outcome correct? Explain.
Test dataTypeExpected outcome
30, 1boundarylamp on
3
This version of the program has its checks in a different order. What does it print when light = −5 and people = 2? Is this better or worse than the improved program? Explain.
Python
1light = int(input("Light level 0 to 100? "))
2people = int(input("People waiting? "))
3if light < 30 and people > 0:
4    print("Shelter lamp on")
5elif light < 0 or light > 100:
6    print("Sensor error")
7else:
8    print("Shelter lamp off")
4
Write two sentences to evaluate the shelter lamp program.