Lesson 6: Magic 8 ball
About this lesson
This is the sixth 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.
Imagine you’ve been tasked with building a new online tool called Doctor Internet.
If someone is feeling unwell, they can answer a few simple questions about symptoms and Doctor Internet will identify their illness and produce a prescription to take to the chemist.
As a class, discuss:
1. What data would this system need access to?
Image credit: jennycepeda/pixabay
- Medical database of diseases, their symptoms and prescriptions
- Patient health background data
- The system would need to be kept up to date with new information
2. What are the risks posed by this system? What could go wrong?
Note: There are numerous very high risks inherent in this system.
- Access to individual health data is a privacy risk and the source of much controversy worldwide.
- The number of factors influencing a diagnosis is high, making the coding an enormous challenge.
- Misdiagnoses could lead to injury or death! These could happen due to lack of information, poor design, or even bugs in the programming.
- Legally, who is responsible if a misdiagnosis is given?
- Would users trust such a system? Trust in large-scale digital systems, such as social media networks, has fallen in recent years.
3. Is it possible to reduce or mitigate the risks?
- The system might be used by medical professionals for the purpose of additional advice.
- Due to having access to a large database of diseases, the system might alert a medical professional to something they otherwise missed.
4. Do solutions like this exist in the real world?
These solutions were much hyped, particularly in the 1980s for business and finance applications. Such solutions were called expert systems. This Wikipedia article explains how the concept went out of favour in the 1990s, but the conditional logic became integrated into other, broader solutions.
Learning map and outcomes
In this lesson, students will:
- practice the new concept of the array length property,
- explore the design of a Magic 8 Ball Decision Maker that gives random advice for a given question from the user,
- plan and code the Magic 8 Ball.
Begin by watching the video introducing the length property:
BEGIN colours ← [‘red’, ‘yellow’, ‘green’, ‘purple’, ‘orange’, ‘blue’] Display “I know this many colours: ”, length of colours Display “The first colour I know is ”, colours Display “The second colour is ”, colours Display “The last colour is ”, colours[length of colours – 1] END
- Finally, add a couple of extra colours to the array, then run your program again. The last colour in the array should still be displayed correctly.
Remember, in Scratch, items in a list are numbered starting from 1. See an example here.
But in Python, list elements are numbered starting from 0. The first entry ‘Canberra’ has position 0. The last entry ‘Rome’ has position 4.
capitals = ['Canberra', 'Paris', 'London', 'Kinchasa', 'Rome'] print(capitals)
This program displays “Rome”.
var capitals = ['Canberra', 'Paris', 'London', 'Kinchasa', 'Rome']; alert(capitals);
It is easy to forget that the position of the last item is 1 less than the array’s length. Off-by-one bugs are among the most common coding bugs, even for experienced programmers.
For more on setting up and choosing a language, see Setting Up.
Step 1: Solution development
This video demonstrates coding a simple decision maker program, then expands it to a Magic 8 Ball program.
Choose two of the links below (eg. Scratch and Python) to find the simple decision maker programs. As in the video, build on those programs so that they now include all of the standard 20 Magic 8 Ball responses.
Step 2: Tinker task
Edit the program to make your Magic 8 Ball optimistic or pessimistic. After asking the first question, the program should ask the user if they want an optimistic or pessimistic response (o or p).
If the user chooses ‘o’, the program chooses randomly from among the first 10 responses. If the user chooses ‘p’, the program chooses randomly from among the last five responses.
These challenges use the skills covered so far. By writing or modifying their own programs, students have an opportunity to demonstrate Application and Creation.
Create a program to help a teacher pick a random student in the class. The program will choose and display a student name from an array of 20 or more names.
Back in Lesson 3: Challenge 1, we used if-else code to make a decision based on our random number, but that was useful only for a small number of names. Now, you can start with an array that includes all the names, so there’s no need for if-else code.
Remember, your program must still work perfectly if the array is different, for example, if you add or remove an extra student name.
Create a program to tell you the country where you can find a famous landmark.
The program should contain two arrays, one with landmarks and another with their countries. The order of each array must match. For example:
|3||Sydney Opera House||3||Australia|
Start by displaying the complete array of landmarks. The user then selects one by entering a number. Finally, the correct country is displayed.
An example of how the program should appear:
Here are the landmarks you can choose from... Eiffel Tower, Big Ben, Borobudur, Sydney Opera House Enter number to learn the country (first is 0): 2 Borobudur is in Indonesia.
Challenge early finishers to make the program more user friendly. User can now enter 1 for the first landmark, instead of 0.
Challenge 3 (Optional)
Create a program to sort a list of TV shows into alphabetical order.
Start with an array of 10 or more TV show names, not in alphabetical order. Display the array as it is.
- In Python:
capitals = ['Canberra', 'Paris', 'London', 'Kinchasa', 'Rome']
var capitals = ['Canberra', 'Paris', 'London', 'Kinchasa', 'Rome'];
Once sorted, display the array again to see the result.
- Setting up online environments
- Online environments for coding in each language
- Cheat sheets listing basic commands for coding: