Python Sorting Hat
In this lesson, students will create a Harry Potter style sorting hat.
Suggested time: 60 mins
Learning Objectives:
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:
KS3:
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.
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:
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:
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:
elif number == 2:
print("Hufflepuff") elif number == 3: print("Ravenclaw") |
Finally, explain to students that, as there is only one more possible result, they can use an else statement to end the condition. Ask students to input the following:
else:
print("Slytherin") |
The student's solutions should look like this:
# Sorting Hat version 1
import random #Pick a number between 1 and 4 number = random.randint(1,4) if number == 1: print("Gryffindor") elif number == 2: print("Hufflepuff") elif number == 3: print("Ravenclaw") else: print("Slytherin") |
Ask students to run their code – making note of and fixing any runtime errors.
Version 2 - Using lists
Pose the question: "What if we had 10 or 20 houses or we wanted to create a student name picker with up to 30 students, what would be the problem with using an if/elif/else statement?".
(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 a list. Explain to students that lists will most likely be covered in their Controlled Assessment / exam.
STEP 1 - Importing the necessary libraries
Start by instructing students to import the random library:
Pose the question: "What if we had 10 or 20 houses or we wanted to create a student name picker with up to 30 students, what would be the problem with using an if/elif/else statement?".
(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 a list. Explain to students that lists will most likely be covered in their Controlled Assessment / exam.
STEP 1 - Importing the necessary libraries
Start by instructing students to import the random library:
import random
|
STEP 2 - Creating the list
Instruct students to create a list containing the four houses ("Slytherin", "Gryffindor", "Hufflepuff" and "Ravenclaw"). Suggest calling the list 'houses'. Their code should look like this:
Instruct students to create a list containing the four houses ("Slytherin", "Gryffindor", "Hufflepuff" and "Ravenclaw"). Suggest calling the list 'houses'. Their code should look like this:
houses=["Slytherin", "Gryffindor", "Hufflepuff", "Ravenclaw"]
|
STEP 3 - Selecting a random item from the list
Finally, ask students to enter the following code:
Finally, ask students to enter the following code:
print(random.choice(houses))
|
The students finished code should look like this:
# Sorting Hat version 1
import random # List of houses. houses=["Slytherin", "Gryffindor", "Hufflepuff", "Ravenclaw"] # Pick a house at random from houses. print(random.choice(houses)) |
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):
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):
The students finished code should look like this:
import tkinter
import random # List of houses. houses=["Slytherin", "Gryffindor", "Hufflepuff", "Ravenclaw"] # Function to pick a house at random from houses. def pickHouse(): hatLabel.configure(text=(random.choice(houses))) # configure the tkinter Graphical User Interface (GUI). root = tkinter.Tk() # Set the tkinter window title. root.title("Python Sorting Hat") # Set the tkinter window size. root.geometry("600x100") # Set the font style and size for tkinter. hatLabel = tkinter.Label(root, text="", font=("Helvetica", 32)) hatLabel.pack() # Create a button with the label 'Choose a house'. insultButton = tkinter.Button(text="Choose a house", command=pickHouse) insultButton.pack() root.mainloop() |
Ask students to run their code – making note of and fixing any runtime errors.
Extension:
Challenge students to:
Challenge students to:
- Change the names of the houses in their sorting hat - if the school operates a house system, students can use their school's houses.
- Create a random name selector that will randomly pick a student in their class.
Resources:
Simple sorting hat solution
|
|
Sorting (List solution)
|
|
tkinter sorting hat solution
|
|
You may also be interested in:
Licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Raspberry Pi "Sorting Hat" lesson is licenced under a Creative Commons Attribution 4.0 International License.