teachwithict
  • Home
  • Teaching & Learning
    • Tools for Teachers >
      • Web 2.0 >
        • Top 10 Web 2.0 Tools
        • Classroom Response Tools
      • Quiz Tools
      • Classroom Management Tools
      • Software >
        • Media Conversion Tools
        • Application Software
      • Multimedia >
        • Free Music
    • Digital Leaders >
      • Digital Leaders Blog >
        • DL Archive
      • DL Tips
      • Quotes
      • Links
    • BYOD
    • Flipped Learning >
      • Flipped Classroom vs. Blended Classroom
    • Gamification >
      • Gamification vs. Game Based Learning vs. Game Design
    • Game Based Learning >
      • Minecraft >
        • Minecraft in Education
        • Minecraft Lesson Ideas
        • Minecraft Literacy
        • Minecraft Coding Tutorials >
          • Minecraft Code Builder Setup
          • Minecraft Code: Walking on Air
          • Minecraft Code: Hello World
          • Minecraft Code: Functions
          • Minecraft Code: Rainbow
          • Minecraft Code: Skyscraper
          • Minecraft Pixel Art
    • The magic of >
      • The magic of Computing
      • The magic of Maths
      • The magic of Science
      • The magic of Literacy
    • Breakout Edu
    • PLN Challenge >
      • Covering your backside
      • Twitter Challenge >
        • Challenge 1
        • Challenge 2
        • Challenge 3
        • Tips for staying safe on Twitter
        • Tips for hosting Edu Twitter Chats
        • Tips for taking part in Twitter chats
        • How to storify a chat
        • How to schedule Tweets
        • Twitter Lists
        • It's all about the hashtag! Essential chats for educators
      • Facebook Challenge >
        • Challenge 4
        • Challenge 5
        • Challenge 6
        • Tips for staying safe on Facebook
      • Google Challenge >
        • Challenge 7
        • Challenge 8
        • Challenge 9
      • Pinterest Challenge >
        • Challenge 10
        • Challenge 11
        • Challenge 12
        • Tips for using Pinterest
      • Blog Challenge >
        • Challenge 13
        • Challenge 14
        • Challenge 15
        • How to create a classroom blog
      • Staffrm Challenge >
        • Challenge 16
        • Challenge 17
        • Challenge 18
      • Skype Challenge >
        • Challenge 19
        • Challenge 20
        • Challenge 21
      • Edmodo Challenge >
        • Challenge 22
        • Challenge 23
        • Challenge 24
      • TeachMeet Challenge >
        • Challenge 25
        • Challenge 26
        • Challenge 27
      • In-school challenge >
        • Challenge 28
        • Challenge 29
        • Challenge 30
      • 30 ways to grow your PLN
    • T&L Blog
  • Computing
    • Computing Blog >
      • GCSE Computer Science
      • General
    • CPD >
      • Blocks to Code
      • Tutorials >
        • How to embed Flash into PowerPoint
        • How to embed YouTube videos in to PowerPoint
        • How to create a wiki
        • How to create a classroom blog
    • Teaching Computer Science >
      • Schemes of Work
      • Programming Resources
      • Theory
      • Posters
    • Theory Lessons >
      • Theory Tests
      • Computer Hardware >
        • Input / Output
        • CPU
        • LMC
        • Storage Devices
        • Memory (RAM / ROM)
      • Binary Lessons >
        • Units of Data
        • Binary Representation
        • Binary Representation of Images
        • Binary Representation of Characters
        • Binary Representation of Sound
        • Binary Representation of Instructions
        • Binary Addition
        • Hexadecimal
      • Software >
        • Operating System
        • Types of Software
        • Utility Software
      • Programming >
        • Flowcharts
    • Game Design
    • App Creation
    • Digital Literacy >
      • Social Media (Good or Bad thing?)
      • Cyberbullying
      • Staying Safe Online
    • ICT Strand >
      • Spreadsheet Modelling
      • Superhero Movie Trailer
      • PowerPoint Task
      • Paint 3D Tutorial
    • Programming Lessons >
      • Microbit >
        • Magic 8 Ball
        • Harry Potter Sorting Hat
        • Music
        • Music (Festive)
        • Quick Start Guide
        • Microbit Simulator
      • TouchDevelop >
        • TouchDevelop Challenge
        • Scheme of Work
        • TouchDevelop Lessons >
          • 8 bit challenge
          • Turing Test
          • Turtle Power
          • 8 Ball
          • Under the hood
        • Tutorials
      • Raspberry Pi >
        • Networking the Pi
        • Minecraft Pi
        • Harry Potter Sorting Hat >
          • Harry Potter Sorting Hat (GPIO)
        • Raspberry Pi LTSP (Networked Classroom)
        • Astro Pi
      • Scratch
      • Python >
        • Chatbot
        • Coding Golf
        • Magic 8 ball
        • Python Mad Libs
        • Shakespearean Insult Generator >
          • Shakespearean Insult Generator Part 2
        • Sorting Hat
      • Little Man Computer
      • Hour of Code >
        • HoC 8-Bit Challenge
  • 21 Things
  • Microbit
    • Physical Computing >
      • micro:bit LEDs
  • About
  • Blog
  • Home
  • Teaching & Learning
    • Tools for Teachers >
      • Web 2.0 >
        • Top 10 Web 2.0 Tools
        • Classroom Response Tools
      • Quiz Tools
      • Classroom Management Tools
      • Software >
        • Media Conversion Tools
        • Application Software
      • Multimedia >
        • Free Music
    • Digital Leaders >
      • Digital Leaders Blog >
        • DL Archive
      • DL Tips
      • Quotes
      • Links
    • BYOD
    • Flipped Learning >
      • Flipped Classroom vs. Blended Classroom
    • Gamification >
      • Gamification vs. Game Based Learning vs. Game Design
    • Game Based Learning >
      • Minecraft >
        • Minecraft in Education
        • Minecraft Lesson Ideas
        • Minecraft Literacy
        • Minecraft Coding Tutorials >
          • Minecraft Code Builder Setup
          • Minecraft Code: Walking on Air
          • Minecraft Code: Hello World
          • Minecraft Code: Functions
          • Minecraft Code: Rainbow
          • Minecraft Code: Skyscraper
          • Minecraft Pixel Art
    • The magic of >
      • The magic of Computing
      • The magic of Maths
      • The magic of Science
      • The magic of Literacy
    • Breakout Edu
    • PLN Challenge >
      • Covering your backside
      • Twitter Challenge >
        • Challenge 1
        • Challenge 2
        • Challenge 3
        • Tips for staying safe on Twitter
        • Tips for hosting Edu Twitter Chats
        • Tips for taking part in Twitter chats
        • How to storify a chat
        • How to schedule Tweets
        • Twitter Lists
        • It's all about the hashtag! Essential chats for educators
      • Facebook Challenge >
        • Challenge 4
        • Challenge 5
        • Challenge 6
        • Tips for staying safe on Facebook
      • Google Challenge >
        • Challenge 7
        • Challenge 8
        • Challenge 9
      • Pinterest Challenge >
        • Challenge 10
        • Challenge 11
        • Challenge 12
        • Tips for using Pinterest
      • Blog Challenge >
        • Challenge 13
        • Challenge 14
        • Challenge 15
        • How to create a classroom blog
      • Staffrm Challenge >
        • Challenge 16
        • Challenge 17
        • Challenge 18
      • Skype Challenge >
        • Challenge 19
        • Challenge 20
        • Challenge 21
      • Edmodo Challenge >
        • Challenge 22
        • Challenge 23
        • Challenge 24
      • TeachMeet Challenge >
        • Challenge 25
        • Challenge 26
        • Challenge 27
      • In-school challenge >
        • Challenge 28
        • Challenge 29
        • Challenge 30
      • 30 ways to grow your PLN
    • T&L Blog
  • Computing
    • Computing Blog >
      • GCSE Computer Science
      • General
    • CPD >
      • Blocks to Code
      • Tutorials >
        • How to embed Flash into PowerPoint
        • How to embed YouTube videos in to PowerPoint
        • How to create a wiki
        • How to create a classroom blog
    • Teaching Computer Science >
      • Schemes of Work
      • Programming Resources
      • Theory
      • Posters
    • Theory Lessons >
      • Theory Tests
      • Computer Hardware >
        • Input / Output
        • CPU
        • LMC
        • Storage Devices
        • Memory (RAM / ROM)
      • Binary Lessons >
        • Units of Data
        • Binary Representation
        • Binary Representation of Images
        • Binary Representation of Characters
        • Binary Representation of Sound
        • Binary Representation of Instructions
        • Binary Addition
        • Hexadecimal
      • Software >
        • Operating System
        • Types of Software
        • Utility Software
      • Programming >
        • Flowcharts
    • Game Design
    • App Creation
    • Digital Literacy >
      • Social Media (Good or Bad thing?)
      • Cyberbullying
      • Staying Safe Online
    • ICT Strand >
      • Spreadsheet Modelling
      • Superhero Movie Trailer
      • PowerPoint Task
      • Paint 3D Tutorial
    • Programming Lessons >
      • Microbit >
        • Magic 8 Ball
        • Harry Potter Sorting Hat
        • Music
        • Music (Festive)
        • Quick Start Guide
        • Microbit Simulator
      • TouchDevelop >
        • TouchDevelop Challenge
        • Scheme of Work
        • TouchDevelop Lessons >
          • 8 bit challenge
          • Turing Test
          • Turtle Power
          • 8 Ball
          • Under the hood
        • Tutorials
      • Raspberry Pi >
        • Networking the Pi
        • Minecraft Pi
        • Harry Potter Sorting Hat >
          • Harry Potter Sorting Hat (GPIO)
        • Raspberry Pi LTSP (Networked Classroom)
        • Astro Pi
      • Scratch
      • Python >
        • Chatbot
        • Coding Golf
        • Magic 8 ball
        • Python Mad Libs
        • Shakespearean Insult Generator >
          • Shakespearean Insult Generator Part 2
        • Sorting Hat
      • Little Man Computer
      • Hour of Code >
        • HoC 8-Bit Challenge
  • 21 Things
  • Microbit
    • Physical Computing >
      • micro:bit LEDs
  • About
  • Blog
Search by typing & pressing enter

YOUR CART

Python Magic 8 Ball

Like this lesson? Share it with your followers by clicking on the image below.
Python Magic 8 Ball Lesson
Creating a “Magic 8 ball game using Python

In this introduction to programming using Python, students will create a “Magic 8-Ball” game. The game will work by asking the user to input a yes / no style question and will respond with one of it’s classic predictions such as: “Yes”, “Most likely” and “Outlook not so good”.

Suggested time: 60 mins

Learning Objectives:
  • Understand and use sequence in an algorithm
  • Understand and use selection in an algorithm (IF, Else and Else if)
  • Understand and data structures in an algorithm (for example, Lists, Tables or Arrays)
  • Understand the importance of comments in code

Curriculum Mapping:

KS2:
  • Design, write and debug programs that accomplish specific goals; solve problems by breaking them into smaller parts. Select, use and combine a variety of software on a range of digital devices to design and create a range of programs.
  • Use sequence, selection and repetition in programs; work with variables and various forms of input and output
  • Use logical reasoning to explain how some simple algorithms work; detect and correct errors in algorithms and programs

KS3:
  • Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems. 
  • Make appropriate use of data structures [for example, lists, tables or arrays]; design and develop modular programs that use procedures or functions

Introduction:

The Magic 8-ball is a fortune telling toy created by Mattel in the 1950s.
Magic 8 Ball
Image by CRASH:Candy, used under a Creative Commons Attribution 2.0 Generic license (Flickr image link)
The concept is simple. Ask the 8-ball a yes or no question and the 8-ball will reply with answers such as “Yes”, “No”, “Without a doubt” etc. (Seemingly being able to predict the future)

The Magic 8 Ball is made up of 20 responses – 10 positive, 5 negative and 5 neutral. The 20 answers are:

Positive answers:

● It is certain ● It is decidedly so ● Without a doubt ● Yes definitely ● You may rely on it ● As I see it, yes ● Most likely ● Outlook good ● Yes ● Signs point to yes

Negative answers:

● Don't count on it ● My reply is no ● My sources say no ● Outlook not so good ● Very doubtful

Neutral answers:

● Reply hazy try again ● Ask again later ● Better not tell you now ● Cannot predict now ● Concentrate and ask again


According to Wikipedia: (http://en.wikipedia.org/wiki/Magic_8-Ball) “Using the coupon collector's problem in probability theory, it can be shown that it takes, on average, 72 outcomes of the Magic 8 Ball for all 20 of its answers to appear at least once.” 

To find out more about the Magic 8-Ball game, click on the links below:
  • Wikipedia (Magic 8-Ball) - http://en.wikipedia.org/wiki/Magic_8-Ball 
  • Magic 8-Ball.org - http://www.magic8ball.org 

​Lesson Outline:

In this introduction to programming using Python, students will create a “Magic 8-Ball” game. The game will work by asking the user to input a yes / no style question and will respond with one of it’s classic predictions such as: “Yes”, “Most likely” and “Outlook not so good”.


Starter:

Direct students to one of the online Magic 8-Ball games listed below. Instruct students to ask the Magic 8-Ball a series of yes / no questions and note down some of the answers. Ask students to note down any repeat answers. 

Note: Use these sites at your own risk. TeachwithICT.weebly.com is not responsible for the content of external Internet sites. 


Online Magic 8-Ball games:

Ask the Magic 8 Ball – http://eightball.tridelphia.net 
The Magic 8-Ball - http://lord.xopl.com/ulpage3a/8-ball.html 
Interactive Magic 8-Ball - http://www.magicmgmt.com/gary/magic8ball/index1.html 

After a few minutes, stop the class and ask them to feedback their findings to the rest of the class.

Note down some of the students’ findings and ask the students how they think the game works. Try to draw out answers such as “answers are randomly generated” and “answers chosen from a list”.

Finally, inform students that they are going to create a Magic 8-Ball game that will appear to predict the future.


STEP 1: Introducing the Random Function
 
Before students create their Magic 8-ball game, they need to become familiar with the random function in Python. 
 
Tell students that they are going to write an algorithm to simulate the flipping of a coin.
 
Instruct students to type in and run the following code:
import random
​​
cointoss = [“Heads”,”Tails”]
print(random.choice(cointoss))
Explain to students that the first line imports the random function needed for the 'coin toss' game to work. Explain that the second line creates a list containing our two possible outcomes from flipping a coin and that the last line selects a random entry from the list (in this case either “Yes” or “No”) and display it on the screen.      

Ask students to comment the code, using the hashtag (#), explaining what the code is doing. Explain that these two lines of code will form the main basis of their ‘Magic 8-ball’ game.
 
Extension:
  • Challenge students to modify their code to simulate the rolling of a dice.
 
​
STEP 2 – Creating a simple Magic 8-Ball game

Before the students start writing the code for their game, they must first import the necessary libraries to make the code work. Tell students that will first need to import the following libraries:  
​​import random
import time 
When creating their Magic 8-Ball game, students will first need to create a list containing all of the 8-ball's possible responses. I suggest starting with just 3 responses: "Yes". "No" and "Maybe".

Instruct students to create a list containing some responses. For example: ​
​responses = ["yes","no","maybe"]
Next, ask students to instruct the user to input a 'Yes/No answer' type question:
​question = input("Please ask me a Yes/No style question ")
Next, tell students that they are going to add in a pause (using the time.sleep() function) to make it look as if the computer is thinking about the answer:
time.sleep(2)
Finally, ask students to write the code to select a response at random from the list:
print(random.choice(responses)) ​
The finished solution will look something like this:
​Extension:
​

Challenge students to:
  1. Comment their code explaining how it works.
  2. Input all of the 20 classic answers. (See above) Hint: Students can change the answers if they wish however, they must keep to the format of 10 positive, 5 negative and 5 neutral answers.
  3. Modify the code so that the pause is random.
  4. Modify their code so that the game repeats (loops) until the user tells it to stop.
Resources:
Example solution
magic_8.zip
File Size: 0 kb
File Type: zip
Download File


Extension - File handling

Having really long lists in Python is not very efficient. Not only that, it makes it more difficult to debug. A much better way to handle the list of responses would be to use a text file.

Ask students to create a text file containing all the 20 possible responses (each on a new line) and save this as 'responses.txt' e.g.
It is certain
It is decidedly so
Without a doubt
Yes definitely
You may rely on it
As I see it, yes
Most likely
Outlook good
Yes
​Signs point to yes
Finally, instruct students to copy the following code (making sure that they read the comments explaining what the code is doing).
​import random
import time

# Open the 8 Ball responses text file
f = open('responses.txt','r')

# Read the whole file and store each line in a list
responses = f.readlines()

# Ask the user to input a Yes/No style question
question = input("Please ask me a question")

# Pause for 2 seconds to make it look like the computer is thinking 
time.sleep(2)

# Print a response at random from the list
print(random.choice(responses)) 

Plenary:

Direct students to swap places with a partner or person next to them and try out their Magic 8-Ball game. Instruct the students to place a comment in their partner’s code suggesting at least one improvement. After a few minutes, ask the students to return to their seats and make any suggested improvements.


Licence:

Unless otherwise specified, everything in this repository is covered by the following licence:
Picture
Python 'Magic 8-Ball' lesson is licenced under a Creative Commons Attribution 4.0 International License.

You may also be interested in:
  • Python Shakespearean insult generator
  • Python Mad Libs
  • Code Golf
  • Python Sorting Hat
  • Python Chat Bot

Tags: coding on computer, computer coding,  best place to learn coding online, online coding, online code, computer programming and Coding, hour of code program, computer science degree programs, programs for computer science, what does coding mean in computers, how does computer code work, what is computer programmer, programming language python, python programming language, python coding language, programming languages python, python code language, python programming language example, what is the python programming language, python scripting, python language tutorial, what type of programming language is Python, python class, python how to learn, learn python, learn python from scratch, where to learn Python, coding on computer, computer coding,  best place to learn coding online, online coding, online code, computer programming and Coding, hour of code program, computer science degree programs, programs for computer science, what does coding mean in computers, how does computer code work, what is computer programmer.
Powered by Create your own unique website with customizable templates.