Lesson 2: Calculator
About this lesson
This is the second in a series of lessons to transition from visual coding to text-based coding with a general-purpose programming language.
Year band: 7-8, 5-6Curriculum Links Assessment
Links with Digital Technologies Curriculum Area
|Processes and Production Skills||5-6||
Design, modify and follow simple algorithms involving sequences of steps, branching, and iteration (repetition) (ACTDIP019)
Design algorithms represented diagrammatically and in English, and trace algorithms to predict output for a given input and to identify errors (ACTDIP029)
Implement and modify programs with user interfaces involving branching, iteration and functions in general-purpose programming language (ACTDIP030)
Students undertake a self-reflection of the programming task. The teacher can use the completed self-assessments to assist in summative assessment.
Use the context of ordering food for lunch to explain (in a practical way) the use of branching.
- You and your friends are having lunch at a local food court.
- Small plate: $8.95
- Large plate: $10.95
- Am I very hungry?
- Do I have enough money for the plate I want?
- After ordering your food, you discover that drinks cost $2.95. Write down some conditions for this second decision.
- Am I thirsty?
- Do I have enough money for the plate and a drink?
- You’ve just made two branching decisions. Here’s pseudocode for an example:
BEGIN If I am very hungry and myMoney >= 10.95 then Say “I’ll have the large plate.” If I am thirsty and myMoney >= (10.95 + 2.95) then Say “And a drink too, please.” End If Else Say “I’ll have the small plate.” If I am thirsty and myMoney >= (8.95 + 2.95) then Say “And a drink too, please.” End If End If END
Image credit: sugree/flickr, Creative Commons BY-SA 2.0
Learning map and outcomes
In this lesson, students will:
- learn how to code decisions with an example about movie ratings
- plan and code a calculator program where the user enters an operation (add, subtract, multiply or divide) as well as two numbers to operate on, then the computer calculates and outputs the result.
As a class, or in teams, design the calculator program as a flowchart. Effective designs may vary, for example, the program might ask for the operation first, in the middle, or last.
Here is one solution (click on the image to expand in a new window):
Image: Flow chart for calculator (simplified)
In the following revised flowchart, we break the big decision into four yes or no decisions. This is closer to how we will code the program (click on the image to expand in a new window).
Image: Flow chart for calculator
Once the flowchart is complete, write the program in pseudocode (structured English). Remember, each of your conditions must come to a yes (true) or a no (false).
BEGIN Display “Choose your operation - a for add, s for subtract, m for multiply, d for divide:” operation ← input from user Display “Enter the first number:” firstNumber ← input from user Display “Enter the second number:” secondNumber ← input from user If operation = “a” then Display “The result is “, firstNumber + secondNumber Else If operation = “s” then Display “The result is “, firstNumber - secondNumber Else If operation = “m” then Display “The result is “, firstNumber * secondNumber Else If operation = “d” then Display “The result is “, firstNumber / secondNumber End if END
Program designers use pseudocode (also called structured English) before coding an algorithm in a real, specific programming language.
The purpose of pseudocode is to clearly understand and communicate an algorithm, regardless of the final language used. Because it translates more readily into real code, it is often more popular than a flowchart.
Pseudocode has few strict rules, but here are some helpful hints to get started:
- Begin your algorithm with BEGIN. End it with END.
- Use a left-pointing arrow (←) to indicate assigning a value to a variable.
eg: income ← 5 × 12 The variable income will now contain the value 60.
At Years 7 and 8, the Australian Digital Technologies curriculum specifies: ‘Design algorithms represented diagrammatically and in English …’ (ACTDIP029). This is further specified as ‘structured English’ in Years 9 and 10 (ACTDIP040).
For more on setting up and choosing a language, see Setting Up.
Step 1: Introducing branching
Decision making is called branching or conditionals.
In general-purpose programming, we do this with IF, THEN and ELSE code structures.
Students may work at vastly different paces when doing general-purpose programming, and some may seem to be quicker at spotting problems than others.
Try these strategies with your students:
- Pedantic computer: Think of the computer as a really pedantic person. It usually won’t cooperate if important punctuation (syntax) is missing, if something is spelled differently in two places, or even with the wrong upper or lower case.
- Pair programming or ‘Ask three before me': Often it just takes a fresh pair of eyes to spot errors or bugs in a program. Before asking the teacher the student checks with other students.
- Trace errors: The computer always runs the program in order, line-by-line, according to the code. Study each line one-by-one and ask, ‘What does this line do?’ Consider writing down the values of variables as you go.
- Tinker: Encourage students to tinker with the code. Ask, ‘What would happen if I changed this?’ Alter a part of the code or add a new feature, predict what might happen, and run it to see. You can always put it back again if something goes wrong.
BEGIN a ← 20 Display a b ← 14 Display b c ← a + b If c > 34 then Display “You win $1 000 000!” Else Display “Better luck next time.” End If If (a + c) = 54 then Display “YOLO” End If END
The following video is a real-world example of how branching is used in a more advanced program. If there is connection to the internet ( if (response.status_code == 220) then proceed, otherwise print(' no connection') and the program stops.
Step 2: Calculator
It’s time to code our calculator. The video above covers the entire process of building the code.
Step 3: Tinker task
Modify your calculator program to allow a fifth operation: power. If the user enters ‘p’ for the operation, the first number is raised to the power of the second number.
eg: if first number is 5 and second number is 3, result is 53 = 125
Note: there is no power block (xy) in Scratch.
These challenges use the skills covered so far. By writing or modifying their own programs, students have an opportunity to demonstrate Application and Creation.
Hi, what’s your name? Dani You’re the best, Dani! Hi, what’s your name? Wei Welcome, Wei!
- Write out the algorithm in pseudocode first.
Challenge early finishers to add a second question about the number of Wi-Fi devices in their home, then give a smart comment based on the value.
Design (flowchart or pseudocode) then code a program to sell you an ice cream.
- First, ask which type of cone (plain = $1, cup = $1.50, waffle = $2).
- Next, ask which flavour (always $2.50, no matter what flavour).
- Next, ask which topping (nuts = 50c, sprinkles = 50c, chocolate sauce = $1).
- Finally, display the total cost.
Code a maths quiz, for example:
What is 2 + 5? 7 That’s right! What is 10 – 5? 5 That’s right! What is 3 * 5? 25 Incorrect. Your total score is 2 out of 3.
Here’s a way to break down this challenge into smaller chunks:
- Ask one question, then check if the user got it right. Test your program.
- Add two more questions to follow the first one. Test your program.
- Include a score variable to keep track of how many questions the user got right.
- (Extension) Can you make your questions unique every time? Check out how to generate random numbers in Lesson 3.
- Setting up online environments
- Online environments for coding in each language
- Cheat sheets listing basic commands for coding:
- Python Cheatsheet (from Grok Learning)