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

How to create a Mad Libs game in Python - Part 1

Like this lesson? Share it with your followers by clicking on the image below.
Picture
​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)

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:

What is a Mad Libs

According to Wikipedia, Mad Libs is a phrasal template word game, usually played at parties, where one player prompts other players for a list of words to substitute blanks in a story before reading out the completed story aloud. Source: https://en.wikipedia.org/wiki/Mad_Libs
 
Example:
 
We start with a story, containing several blanked out words. Here’s a basic example:
 
Rumble in the  _______.
 
We then prompt the user to fill in the gaps, in this case, with a noun or place. We might end up with something like this:

Rumble in the Toilet.
 
Obviously, the more entertaining the original story, the better the laughs will be at the end!
 
To find out more, visit the Mad Libs wiki page: en.wikipedia.org/wiki/Mad_Libs

Lesson Outline:

In this lesson, students will code a “Mad Lib” game in Python. The game will work by prompting the user to enter some words (e.g. person’s name, noun, adjective, place, object etc.) and substitute these with blanks in a story.


Starter:

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

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

Online Mad Lib games:
  • Wacky Web Tales – https://www.eduplace.com/tales/
  • Mad Libs - http://www.madglibs.com/ (Click on each tile to reveal a mad lib).
 
 
OPTION 2
 
Print out some example mab libs from madlibs.com and give to students to complete in small groups - http://www.madlibs.com/printables/ Tip: I find that the Mad Libs ‘Hall passes’ are always a hit: http://www.madlibs.com/content/uploads/2016/04/KIDS-excuse-hall-pass_1_.pdf
 
 
OPTION 3
 
Display the mad libs starter PowerPoint (below) on the board and ask students to suggest words to fill in the blanks. You could get the class to vote for their favourite suggestions.

Once students have completed the starter activity, inform them that they are going to create a variation of the Mad Libs format using Python.

Resources:
mad_libs_starter.pptx
File Size: 1049 kb
File Type: pptx
Download File


​Activity 1 – First Mad Lib game
 
There are several ways to create a Mad Lib game in Python. In our version, the program will pick a story at random from a list however, before we start coding our “Ultimate Mad Lib” game, let’s start with a simple version:
 
Step 1. Creating your first Mad Lib
 
Instruct students to type in and run the following code:
​noun = input("Enter a noun: ")
 
print("Rumble in the " + noun)
Extension:
 
Challenge students to:
  • comment their code, using the hashtag (#), explaining what the code is doing.
  • create their own mad lib (If students struggle to come up with their own Mad Libs, they can use the examples from the starter activity).
  • create a Mad Lib that contains 2 or even 3 user inputs.
 
Example:
​noun = input("Enter an animal: ")
noun2 = input("Enter another animal: ")
noun3 = input("Enter an object: ")                                                    
 
print("The " + noun + " and the " + noun2 + " went to sea in a beautiful pea green noun3.")

Activity 2 – Mad Lib (Version 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.
 
Step 1. Introducing the ‘String replace method’
 
Before students create their text file, introduce them to the ‘string replace method’.
 
Sometimes in Python, you find yourself in a situation where you want to modify the contents of string by replacing one piece of text with another. Luckily, Python makes this job easy thanks to the string replace method.
 
1. To understand how the .replace() method works, ask students to try the following code:
​string = "This is a string"
 
string = string.replace("is", "was")
 
print(string)
2. Ask students to comment their code to explain what it is doing.
 
Explain to students that the line ‘string = string.replace(“is”, “was”) looks for the word “is” in the sentence and replaces it with the word “was”. Tell students that they will use the .replace() method to replace the blank(s) in their Mad Libs with the word(s) input by the user.
 
 
Step 2. Creating the text file
 
Next, the students need to create a text file for their Mad Libs– suggest saving the file madlibs.txt. Challenge students to make up some stories and store each one on a different line – instruct students to type in the word ‘blank’ for the word they want to be replaced, for example:
It don't mean a thing if it ain't got that blank!
Rumble in the blank!
I can't start my day without my blank!
Step 3. Writing the code
 
1. Instruct students to copy the following code (making sure that they read the comments explaining what the code is doing):
​# Program to read a random Mad Lib from a file and print the Mad Lib with the user's response
import random
 
# Open the Mad Libs text file
f = open('madlibs.txt','r')
 
# Read the whole file and store each line in a list
madlibText = f.readlines()
 
# Choose a random line from the list
madlib = random.choice(madlibText)
 
# Ask the user to input a noun
noun = input("Enter a noun: ")
 
# Replace the blank with the user's input
madlib = madlib.replace("blank", noun)
 
# Print out the Mad Lib including the user's response
print(madlib)
2. Ask students to run their code – making note of and fixing any runtime errors.
 
Extension:
 
Challenge students to:
  • modify their code so that it displays the randomly chosen Mad Lib first before prompting the user for their response.

Resources:

Mad Lib Solution
madlibs_text.zip
File Size: 0 kb
File Type: zip
Download File


Creating a Mad Libs game in Python - Part 2 (Coming soon!)


You may also be interested in:
  • Python Shakespearean insult generator
  • Python Chat Bot
  • Code Golf
  • Python Sorting Hat
  • Python Magic 8-Ball ​
Powered by Create your own unique website with customizable templates.