# Visual to text codingLesson 6: Magic 8 Ball

Years 5-6; 7-8

### Learning hook

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?
2. What are the risks posed by this system? What could go wrong?

Note: There are numerous very high risks inherent in this system.

3. Is it possible to reduce or mitigate the risks?
4. Do solutions like this exist in the real world?

Image credit: jennycepeda/pixabay

## Learning map and outcomes

In this lesson, students will:

1. access an online programming environment for visual code (Scratch) and for general purpose programming (Python or JavaScript)
2. practice the new concept of the array length property,
3. explore the design of a Magic 8 Ball Decision Maker that gives random advice for a given question from the user,
4. plan and code the Magic 8 Ball.

## Learning input

1. Begin by watching the video introducing the length property: The video only demonstrates JavaScript.

• Scratch has an orange block to do the job (try code):
• Python has a simple command, len() (try code):

• words = ['dark', 'ghastly', 'light', 'bones', 'monkey']

no_of_items = len(words)

print('The number of items in the array is', no_of_items)

1. Carefully read the pseudocode below, then type up and test the program in Scratch as well as JavaScript or Python:

```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[0]
Display “The second colour is ”, colours[1]
Display “The last colour is ”, colours[length of colours – 1]
END
```

2. 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.

## Learning construction

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.

## 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.

1. 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.

#### Solution code:

1. 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:

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.

#### Solution code:

1. (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.

Both Python and JavaScript come with simple sort commands to sort an array:

• In Python:

capitals = ['Canberra', 'Paris', 'London', 'Kinchasa', 'Rome']

capitals.sort()

• In JavaScript:

var capitals = ['Canberra', 'Paris', 'London', 'Kinchasa', 'Rome'];

capitals.sort();

1. Once sorted, display the array again to see the result.

## Resources

• Setting up online environments
• Online environments for coding in each language
• Scratch
• repl.it: an online environment suited to Python
• JSFiddle: an online environment suited to JavaScript
• 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.)