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.

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.

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!

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 lineline = file.readline()

# repeat as long as the line is not emptywhile 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 searchingfile = open("minions.txt","r")

# read the first lineline = file.readline()

# repeat as long as the line is not emptywhile line != "": # names.append(line)

# read the next line line = file.readline()

# close the filefile.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:

import random

names=[]

# open the file and start searchingfile = open("minions.txt","r")

# read the first lineline = file.readline()

# repeat as long as the line is not emptywhile line != "":

# names.append(line)

# read the next line line = file.readline()

# close the filefile.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.

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: