1 of 8

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

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.

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

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:

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.

2 of 8

The finished solution will look like this:

3 of 8

How to create a Mad Libs game in Python part 1

Suggested time: 60 mins

Learning Objectives:

  • Understand and use sequence in an algorithm
  • Understand and use iteration in an algorithm (FOR and WHILE loops)
  • Understand and use data structures in an algorithm (for example, Lists, Tables or Arrays)

Starter:

Direct students to one of the online Mad Lib games listed below. Ask students to submit random words (as instructed) and note down some of the answers. Ask students to note down any unusual answers.

Online Mad Lib games:

4 of 8

How to create a Mad Libs game in Python part 2

In the next example students are going to create a text file containing several mad libs. Just like the previous example, the program will ask the user to enter one or several nouns, verbs, adjectives etc. but this time will pick a Mad Lib at random from a text file.

5 of 8

Python Sorting Hat

In this lesson, students will create a Harry Potter style sorting hat.

Suggested time: 60 mins

Learning Objectives:

  • Understand and use sequence in an algorithm
  • Understand and use iteration in an algorithm (FOR and WHILE loops)
  • Understand and use selection in an algorithm (IF, Else and Else if)​

6 of 8

7 of 8

Creating a Shakespearean Insult Generator

Suggested time: 60 mins

Learning Objectives:

  • Understand and use sequence in an algorithm
  • Understand and use iteration in an algorithm (FOR and WHILE loops)
  • Understand and use selection in an algorithm (IF, Else and Else if)
  • Understand and use data structures in an algorithm (for example, Lists, Tables or Arrays)

Introduction:

How to make an Insult

To make an insult, pick one word from each of the columns (below), combine them to make a sentence and add the word “Thou” at the beginning. For example: If we were to take the first word from each of the 3 columns we would get:

(http://www.pangloss.com/seidel/Shaker/)

8 of 8