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-6
Curriculum Links AssessmentCurriculum Links
Links with Digital Technologies Curriculum Area
Strand | Year | Content Description |
---|---|---|
Processes and Production Skills | 5-6 |
Design algorithms involving multiple alternatives (branching) and iteration (AC9TDI6P02). |
7-8 |
Design algorithms involving nested control structures and represent them using flowcharts and pseudocode (AC9TDI8P05). Trace algorithms to predict output for a given input and to identify errors (AC9TDI8P06). |
Assessment
Students undertake a self-reflection of the programming task. The teacher can use the completed self-assessments to assist in summative assessment.
In assessing code in languages like Python or JavaScript, consider a rubric that includes important skills for general-purpose programming.
Learning hook
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:
- access an online programming environment for visual code (Scratch) and for general-purpose programming (Python or JavaScript)
- 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.
Learning input
Step 1
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)
Step 2
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
Step 3
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).
Learning construction
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:
- Cheat sheets bring together the most basic Python commands or JavaScript commands. See Resources at the bottom of this page.
- 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.
Have students practice branching. Interpret the following pseudocode and code it in Scratch and either Python or Javascript. Students should predict the output of the program (what it will display) before doing their code.
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.
Solution Code
Step 2: Calculator
It’s time to code our calculator. The video above covers the entire process of building the code.
Solution 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
You’ll need to research how to perform this operation in Python or JavaScript. See the 'Resources' section at the bottom for cheat sheets.
Note: there is no power block (xy) in Scratch.
Solution Code
Challenge
These challenges use the skills covered so far. By writing or modifying their own programs, students have an opportunity to demonstrate Application and Creation.
Challenge 1
Code a new program in Python or JavaScript to ask for the user’s name, then give a custom reply depending on the name entered. For example:
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.
- If it helps, code the program in Scratch before going on to Python or JavaScript.
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.
Challenge 2
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.
Challenge 3
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.
Resources
- Setting up online environments
- Online environments for coding in each language
- Cheat sheets listing basic commands for coding:
- Python Cheatsheet (from Grok Learning)
- JavaScript CheatSheet (Tip: Press the little blue tabs to move Variables, Basics, Strings and Data Types to the top.)