Shakespearean Insult Generator - Part 2 (Introducing basic File Handling in Python)
Suggested time: 60 mins
Learning Objectives:
Curriculum Mapping:
KS2:
KS3:
Introduction:
In the previous lesson we explored the use of lists to create a python version of the online “Shakespearean Insult Generator”. This lesson carries on from the previous lesson but explores how to improve upon the program by introducing some simple file handling.
Lesson Outline:
In this lesson, students will improve their Shapespearean Insult Generator by using some simple file handling.
Starter:
Download the following sample code containing an extremely long list and display the code on the board.
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)
- Understand how to handle different types of files
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:
In the previous lesson we explored the use of lists to create a python version of the online “Shakespearean Insult Generator”. This lesson carries on from the previous lesson but explores how to improve upon the program by introducing some simple file handling.
Lesson Outline:
In this lesson, students will improve their Shapespearean Insult Generator by using some simple file handling.
Starter:
Download the following sample code containing an extremely long list and display the code on the board.
very_long_list.py |
Ask students what is the problem with using lists for this amount of data. (Try to draw out answers such as “Easy to make mistakes” and “Difficult to debug” etc.)
Ask students if they can think of a better way to do this?
Explain to students that there is a better way to tackle this task using file handling. Explain to students that file handling will most likely be covered in their Controlled Assessment.
Ask students if they can think of a better way to do this?
Explain to students that there is a better way to tackle this task using file handling. Explain to students that file handling will most likely be covered in their Controlled Assessment.
Activity 1: Introduction to file handling
Before students make improvement to their “Shakespearean Insult Generator”, they need to become familiar with how to safely handle files in Python.
WARNING! This section can cause serious damage to your files if you’re not careful!
To introduce students to basis file handling, let’s re-visit our Minion name generator from the previous lesson!
Before students make improvement to their “Shakespearean Insult Generator”, they need to become familiar with how to safely handle files in Python.
WARNING! This section can cause serious damage to your files if you’re not careful!
To introduce students to basis file handling, let’s re-visit our Minion name generator from the previous lesson!
Opening and reading from a text file (Adapted from Mark Clarkson’s Introduction to Python 2.1 handbook: http://pi.mwclarkson.co.uk/2013/Intro/Introduction_%20to%20Python%20v2.1.pdf
To open a file, you need to make it a variable first. We do this with the
following command:
f = open("filename","mode")
Where filename is the name of the file and the mode must be one of the following:
r read only
w write only - NOTE, this destroys any previous version of the file!
r+ read AND write
a append mode (will write only after the existing data)
1. Instruct students to download the list of minion names file (minions.txt) below:
To open a file, you need to make it a variable first. We do this with the
following command:
f = open("filename","mode")
Where filename is the name of the file and the mode must be one of the following:
r read only
w write only - NOTE, this destroys any previous version of the file!
r+ read AND write
a append mode (will write only after the existing data)
1. Instruct students to download the list of minion names file (minions.txt) below:
minions.txt |
2. Ask students to type in and run the following code:
IMPORTANT! Students must save their Python script in the same folder that they saved the minions.txt file.
#
file = open("minions.txt","r") # read the first line line = file.readline() # repeat as long as the line is not empty while line != "": # print(line) # read the next line line = file.readline() # file.close() |
3. Ask students to fill in the blank comments (#), explaining what the code is doing.
Now that students know how to open a simple text file and print the contents, next they need to know how to store each line of the file in a list so as to manipulate the data later.
4. Instruct students to add the following line at the beginning of their code:
names=[ ]
This new line will create an empty list to store our minion mames.
5. Next, instruct students to replace the line print(line) with the following code:
names.append(line)
Inform students that this new code will add the contents of each line of the text file to the newly created list (names)
Their code should look like this:
names=[]
# open the file and start searching file = open("minions.txt","r") # read the first line line = file.readline() # repeat as long as the line is not empty while line != "": # names.append(line) # read the next line line = file.readline() # close the file file.close() |
6. Finally, tell students that they need to add the code to pick a name at random.
Instruct students to add the following code onto the end of their script:
print("Minion" + " " + random.choice(names))
Hint: Ask students what needs to be imported in order for the following code to work! If the students can’t remember, remind them that they have to add the line: import random at the beginning of their script.
Their final code should look like this:
Instruct students to add the following code onto the end of their script:
print("Minion" + " " + random.choice(names))
Hint: Ask students what needs to be imported in order for the following code to work! If the students can’t remember, remind them that they have to add the line: import random at the beginning of their script.
Their final code should look like this:
import random
names=[] # open the file and start searching file = open("minions.txt","r") # read the first line line = file.readline() # repeat as long as the line is not empty while line != "": # names.append(line) # read the next line line = file.readline() # close the file file.close() print("Minion" + " " + random.choice(names)) |
Extension:
Ask students to create a text file with their own list of names (Perhaps the names of all the students in their class) and test it with their script.
Ask students to create a text file with their own list of names (Perhaps the names of all the students in their class) and test it with their script.
Activity 2: Improving the Insult generator
Remind students how the online Shakespearean Insult generator works (see Part 1). Explain to students that they are going to improve their script by incorporating some simple file handling.
Instruct students to, using what they have learnt and using the resources at their disposal, update their Shakespearean Insult generator from the previous lesson so that it reads the list of random insults from a file.
Hint: Students will need to have 3 separate files (One for each column) and 3 empty lists.
Tip: If you prefer, you may wish to provide students with the lists of names in .txt format – this will save the students from having to type all the names themselves:
Remind students how the online Shakespearean Insult generator works (see Part 1). Explain to students that they are going to improve their script by incorporating some simple file handling.
Instruct students to, using what they have learnt and using the resources at their disposal, update their Shakespearean Insult generator from the previous lesson so that it reads the list of random insults from a file.
Hint: Students will need to have 3 separate files (One for each column) and 3 empty lists.
Tip: If you prefer, you may wish to provide students with the lists of names in .txt format – this will save the students from having to type all the names themselves:
column1.txt |
column2.txt |
column3.txt |
Example Solutions
insult_creator_v1_txt.py |
insult_creator_v1_txt_tkinter.py |
Unless otherwise specified, everything in this repository is covered by the following licence:
The Shakespearean Insult Generator is licenced under a Creative Commons Attribution 4.0 International License.
Based on the Shakespearean Insulter: http://www.pangloss.com/seidel/Shaker/
Based on the Shakespearean Insulter: http://www.pangloss.com/seidel/Shaker/
Tags: 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.