Back

Activity 1.6: Ask, Compute, Answer

divider

Activity 1.6

Ask, Compute, Answer

Key Concepts

Nothing New Today

The Shape of a Program

Testing With Awkward Numbers

No New Ideas

Everything today is something you already did in the last two sessions.

Putting them together is the work.

Almost Every Program Has This Shape

-- 1. ask
-- 2. work it out
-- 3. say the answer

Almost Every Program Has This Shape

-- 1. ask
-- 2. work it out
-- 3. say the answer

Ask for what you do not know. Work out what follows from it. Say the answer in a sentence a person would say.

Your Create Task program in Quarter 3 will be this shape, with more in the middle.

Write the Asking First

Ask the questions, print the answers straight back, and run it before you write any arithmetic.

If the input is wrong, every calculation after it is wrong for a reason that has nothing to do with the calculation.

Then Test It Where It Hurts

  • A number that divides exactly
  • A number that does not
  • Zero
  • One

Anyone can test a program with the number they had in mind while writing it.

Today's Objectives

  • Plan a program before typing it
  • Collect three numbers from the user
  • Work out a result with math.floor and %
  • Test it with values you did not design it for

Key Terms

Input
What the program asks for and does not decide itself.
Output
What it says back.
Test case
One set of inputs, with the answer you expect before you run it.
Edge case
A test at the awkward end — zero, one, or exactly divisible.

'F' → Fullscreen

divider

Build

You are writing a program that works out how much pizza to order. It asks three questions and answers in two sentences.

Task 1: Plan It on Paper

Before you open VS Code. On your sheet, write down:

  • The three questions the program asks
  • The arithmetic that turns those three numbers into an answer
  • The two sentences it prints, with the blanks marked

Then work out one example by hand: 9 people, 3 slices each, 8 slices per pizza. Write the answer on your sheet before the computer tells you.

Task 2: Ask the Three Questions

Copy template-console to 1-06-pizza. Write only the asking, and print the three answers straight back:

main.lua
io.write("How many people? ")
local people = tonumber(io.read())
io.write("Slices each? ")
local slicesEach = tonumber(io.read())
io.write("Slices per pizza? ")
local perPizza = tonumber(io.read())
print(people)
print(slicesEach)
print(perPizza)
Terminal
How many people? 9 [Enter]
Slices each? 3 [Enter]
Slices per pizza? 8 [Enter]
9
3
8

Run it. Three numbers in, the same three numbers out. That is all this step has to prove, and it is worth proving before anything depends on it.

Task 3: Work It Out and Say It

Delete the three plain print lines and put the real work in their place:

main.lua
io.write("How many people? ")
local people = tonumber(io.read())
io.write("Slices each? ")
local slicesEach = tonumber(io.read())
io.write("Slices per pizza? ")
local perPizza = tonumber(io.read())
local total = people * slicesEach
local pizzas = math.floor(total / perPizza)
local extra = total % perPizza
print("You need " .. total .. " slices.")
print("That is " .. pizzas .. " whole pizzas plus " .. extra .. " slices.")
Terminal
How many people? 9 [Enter]
Slices each? 3 [Enter]
Slices per pizza? 8 [Enter]
You need 27 slices.
That is 3 whole pizzas plus 3 slices.

Compare that against the answer you worked out by hand in task 1. If they disagree, one of you is wrong and it is worth finding out which.

Task 4: Test It Where It Hurts

Run it four more times and write every result on your sheet before you run it:

PeopleSlices eachPer pizzaWhy this one
1628Divides exactly
138Not even one pizza
038Nobody came
421Very small pizzas
Terminal
How many people? 16 [Enter]
Slices each? 2 [Enter]
Slices per pizza? 8 [Enter]
You need 32 slices.
That is 4 whole pizzas plus 0 slices.

Three of those four are arithmetically correct and read badly. Find them, and for each one write down what a person would rather have been told.

Challenge (Optional): How Many to Actually Buy

The program says 3 whole pizzas plus 3 slices, but nobody sells three slices. Add a line that says how many pizzas to order so that everybody gets fed.

You have the tools already. The hard part is that the answer is not always pizzas + 1 — test it with 16 people at 2 slices each and see.

Challenge (Optional): Your Own Calculator

Copy the template to 1-06-mine and write a program of the same shape about something you care about — splitting a bill, minutes of practice per week, how many packs of cards to buy, how much of a video game you have finished.

Three questions, real arithmetic, and an answer in a full sentence. Test it with zero and with one.

divider

Checkpoint

  • Your paper plan is on the sheet, written before the code
  • 1-06-pizza asks three questions and answers in two sentences
  • All four test results are recorded, with your predictions
  • You can name the three tests whose wording reads badly
divider

Reflection

Answer the following questions before submitting your work.

  1. You printed the three inputs straight back before writing any arithmetic. Describe a specific bug that step would have caught, and how long it would have taken to find without it.
  2. Three of your four test cases produced correct numbers in a sentence that reads badly. Pick the worst one, say what it printed, and say what you would want it to print instead.
  3. You wrote the expected answer down before running each test. Explain what that habit protects you from, given that you could just read the answer off the screen.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete