Python Sorting Hat
Learn how to create a 'Harry Potter' style sorting hat with this free Python web tutorial.
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 tutorial, you will learn how to 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 tutorial, you will learn how to create a Harry Potter style sorting hat using the tkinter GUI in Python.
The 'Sorting Hat' scene from Harry Potter and the philosopher's Stone:
Part 1 - Introducing Randomness
Let's first start with as simple dice roll simulator.
STEP 1 - Importing the necessary libraries
In order for the program to work, you will need to import the random library. Input the following code:
Let's first start with as simple dice roll simulator.
STEP 1 - Importing the necessary libraries
In order for the program to work, you will need to import the random library. Input the following code:
Use the code window (below) to test your code.
import random
|
Next, you need to write the code to pick a number at random. Add the following code on a new line:
number = random.randint(1,6)
|
Next, we need to create a condition where if the random number is generated is a '1' the program will return "One": For this, we will need to use an if() statement:
if number == 1:
print("One") |
Next, let's add a condition for if the program randomly generates a 2 or a 3. For this, we will need to use an elif statement. Add the following lines of code:
elif number == 2:
print("Two") elif number == 3: print("Three") |
Repeat this for sides "Four" and "Five" of the dice.
Finally, as there is only one more possible result, we can use an else statement to end the condition. Add the following:
Finally, as there is only one more possible result, we can use an else statement to end the condition. Add the following:
else:
print("Six") |
Run your code and add comments, using the # symbol, to explain what the code is doing.
Challenge
Challenge
- Modify you code so that it selects the four houses from Harry Potter:
- Gryffindor
- Hufflepuff
- Ravenclaw
- Slytherin
Part 2 - Introducing lists
What if we had 10 or 20 houses or we wanted to create a student name picker with up to 30 students? Can you see a problem with using an if/elif/else statement?"
There is a better way!
Instead of using multiple if/elif/else statements, we can use a list.
STEP 1 - Importing the necessary libraries
Let's start by importing the random library:
What if we had 10 or 20 houses or we wanted to create a student name picker with up to 30 students? Can you see a problem with using an if/elif/else statement?"
There is a better way!
Instead of using multiple if/elif/else statements, we can use a list.
STEP 1 - Importing the necessary libraries
Let's start by importing the random library:
import random
|
STEP 2 - Creating the list
Next, let's create a simple coin toss game using lists. Add the following:
Next, let's create a simple coin toss game using lists. Add the following:
coinToss=["Heads", "Tails"]
|
STEP 3 - Selecting a random item from the list
Finally, enter the following code to select an item from the list at random:
Finally, enter the following code to select an item from the list at random:
print(random.choice(coinToss))
|
Use the code window above to test your code.
Run your code and add comments, using the # symbol, to explain what the code is doing.
Challenge
Run your code and add comments, using the # symbol, to explain what the code is doing.
Challenge
- Modify you code so that it selects the four houses from Harry Potter:
- Gryffindor
- Hufflepuff
- Ravenclaw
- Slytherin
Extra Challenge
- Comment your code explaining what each line is doing.
- Add your own houses.
- Add a random pause so that appears that the hat is thinking about which house to put you in.
Hint: You will need to 'import time' and use the sleep command: time.sleep(2) - Modify your code so that the game repeats (loops) until the user tells it to stop.
- Modify your code so that it becomes a random name picker for your 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:
Python "Sorting Hat" lesson is licenced under a Creative Commons Attribution 4.0 International License.
Keywords:
Python web, python programming language, python programming, python coding.
Python web, python programming language, python programming, python coding.