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)​

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.

Introduction:

The Sorting Hat is an artefact used at Hogwarts in the Harry Potter books. The hat determines which of the four school houses (Gryffindoe, Slytherin, Hufflepuff and Ravenclaw) each new student is assigned to. The hat resembles a wide-brimmed wizard's hat, with folds and tears that make it appear to have eyes and a mouth. During the opening banquet at the beginning of each school year, the Hat is placed on every first-year student's head. The Hat announces its choice aloud, and the student joins the selected house.

In this lesson, students will create a Harry Potter style sorting hat using the tkinter GUI in Python.

Starter:

Show students the 'Sorting Hat' scene from Harry Potter and the Sorcerer's Stone to set the scene:

Explain to students that they will be creating their very own 'Harry Potter' style sorting hat that will select a house at random when a button is pressed.

Version 1 - Simple sorting hat

Let's first start with as simple version of the sorting hat.

​STEP 1 - Importing the necessary libraries

Tell students that, in order for the program to work, they need to import the random library. Instruct students to input the following code:

​import random

Next, tell students that they need to pick a number at random. Ask students to add the following code on a new line:

​number = random.randint(1,4)

Next, tell students that they need to create a condition where if the random number is generated is a '1' the program will return "Gryffindor": Explain to students that they will need to use an if() statement:

​if number == 1:print("Gryffindor")

Next, tell students that they need to add a condition for if the program randomly generates a 2 or a 3. Explain to students that they will need to use an elif statement. Instruct students to add the following lines of code:

​​​Ask students to run their code – making note of and fixing any runtime errors.

Version 3 - Introducing tkinter

Tkinter is the default GUI that is shipped with Python. With tkinter, it is easy to create GUIs to use with your Python code such as windows and buttons. The following code is a variation of the previous example but using tkinter.

Instruct students to copy the following code (making sure that they read the comments explaining what the code is doing):