Python Magic 8 Ball
Magic 8-ball Tutorial
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.
STUDENT: COMPUTATIONAL THINKER:
- 5a: Students break problems into component parts, extract key information, and develop descriptive models to understand complex systems or facilitate problem-solving.
- 5c: Students break problems into component parts, extract key information, and develop descriptive models to understand complex systems or facilitate problem-solving.
- 5d: Students understand how automation works and use algorithmic thinking to develop a sequence of steps to create and test automated solutions.
EDUCATOR: COMPUTATIONAL THINKING COMPETENCIES:
- 4b: Design authentic learning activities that ask students to leverage a design process to solve problems with awareness of technical and human constraints and defend their design choices.
COMPUTER SCIENCE EDUCATORS:
- 2a: Plan and teach computer science lessons/units using effective and engaging practices and methodologies:
i. Select a variety of real-world computing problems and project-based methodologies that support active and authentic learning and provide opportunities for creative and innovative thinking and problem solving
ii. Demonstrate the use of a variety of collaborative groupings in lesson plans/units and assessments
iii. Design activities that require students to effectively describe computing artifacts and communicate results using multiple forms of media
iv. Develop lessons and methods that engage and empower learners from diverse cultural and linguistic backgrounds
v. Identify problematic concepts and constructs in computer science and appropriate strategies to address them
vi. Design and implement developmentally appropriate learning opportunities supporting the diverse needs of all learners
vii. Create and implement multiple forms of assessment and use resulting data to capture student learning, provide remediation and shape classroom instruction
CSTA K–12 CS Standards:
- 1B-AP-08: Compare and refine multiple algorithms for the same task and determine which is the most appropriate.
- 1B-AP-09: Create programs that use variables to store and modify data.
- 1B-AP-10: Create programs that include sequences, events, loops, and conditionals.
- 1B-AP-11: Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process.
- 1B-AP-13: Use an iterative process to plan the development of a program by including others' perspectives and considering user preferences.
- 1B-AP-15: Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended.
- 1B-AP-17: Describe choices made during program development using code comments, presentations, and demonstrations.
- 2-AP-11: Create clearly named variables that represent different data types and perform operations on their values
- 2-AP-12: Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
- 2-AP-15: Seek and incorporate feedback from team members and users to refine a solution that meets user needs.
- 2-AP-16: Incorporate existing code, media, and libraries into original programs, and give attribution.
- 2-AP-17: Systematically test and refine programs using a range of test cases.
- 3A-AP-14: Use lists to simplify solutions, generalizing computational problems instead of repeatedly using simple variables.
- 3A-AP-15: Justify the selection of specific control structures when tradeoffs involve implementation, readability, and program performance, and explain the benefits and drawbacks of choices made.
AREA OF LEARNING AND EXPERIENCE: Science and Technology:
Computation is the foundation for our digital world.
Progression step 3
- I can use conditional statements to add control and decision-making to algorithms.
- I can identify repeating patterns and use loops to make my algorithms more concise.
- I can explain and debug algorithms.
Progression step 4
- I can decompose given problems and select appropriate constructs to express solutions in a variety of environments.
- I can select and use data structures that efficiently manage data in algorithms.
- I can plan and implement test strategies to identify errors in programs.
Progression step 5
- I can identify, define and decompose problems, choose appropriate constructs and express solutions in a variety of environments.
- I can use file-handling techniques to manipulate data in algorithms.
- I can test, evaluate and improve a solution in software.
The Magic 8-ball is a fortune telling toy created by Mattel in the 1950s.
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
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)) |
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 |
Instruct students to create a list containing some responses. For example:
responses = ["yes","no","maybe"]
|
question = input("Please ask me a Yes/No style question ")
|
time.sleep(2)
|
print(random.choice(responses))
|
Challenge students to:
- Comment their code explaining how it works.
- 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.
- Modify the code so that the pause is random.
- Modify their code so that the game repeats (loops) until the user tells it to stop.
Example solution
|
|
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 |
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)) |
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: