# Hogwarts Sorting Hat Algorithm

In this challenge, we will implement an algorithm to help the sorting hat at Hogwarts school of witchcraft and wizardry decide which house you would more likely belong to.

When a new student join Hogwarts school, they are assigned to one of the four school houses depending on their personal qualities. The four houses are:

• Gryffindor values bravery, loyalty and chivalry
• Ravenclaw values curious minds, creativity and inventiveness
• Hufflepuff values kindness, friendliness, patience
• Slytherin values ambition and competitiveness

Now let’s look at our algorithm step by step using a flowchart that we will convert into a Python program.

Our algorithm will work by asking the end-user some yes/no questions to help define their personality and main qualities. Based on the answers provided by the end-user, the algorithm will allocate points to four score variables, one for each house.

At the end, after collecting all the end-user’s answers, the algorithm will find which house scored the most point and inform the end-user of the house that is a best match to their personality.

#### Step 1: Initialising the Sorting Hat Algorithm

FlowchartPython Code
Flowchart

Python Code

```# Hogwarts Sorting Hat Algorithm

# Step 1: Initialise the Sorting Hat
print("~~~~~~~~~~~~~~~~~~~~~~~")
print("~                     ~")
print("~   The Sorting Hat   ~")
print("~                     ~")
print("~~~~~~~~~~~~~~~~~~~~~~~")
print("")
print("Welcome to Hogwarts school of witchcraft and wizardry!")
print("")
gryffindor = 0
hufflepuff = 0
ravenclaw = 0
slytherin = 0

```

#### Python Code

Complete the Python code below…

#### Step 2: Asking Yes/NO Question

Using IF statements
Let’s ask our first question to see if the user is considering themselves as being a brave person. If so we will add one point to their score for Gryffindor as bravery is a trait of the Gryffindor house.

FlowchartPython Code
Flowchart

Python Code

```# Step 2: Collect user inputs
brave = input("Are you a brave person?").lower()
if brave=="yes":
gryffindor = gryffindor + 1```
Using IF and ELSE statements
Now let’s consider another question which could lead to two outcomes depending on the user’s answer. For instance let’s ask if the user considers themselves as being patient. If the answer is “yes” we will increase their Hufflepuff score by one however, if their answer is “no” we will increase their Slytherin score by 1 as students from the Slytherin students tends to be rather impatient.

FlowchartPython Code
Flowchart

Python Code

```patient = input("Would you describe yourself as being patient?").lower()
if patient=="yes":
hufflepuff = hufflepuff + 1
else:
slytherin = slytherin + 1 ```

#### Step 3: Adding more questions

It’s now your turn to work out the Python code needed to add the following 8 questions:

Question 3Question 4Question 5Question 6Question 7Question 8Question 9Question 10

#### Step 4: Displaying the scores for each house

FlowchartPython Code
Python Code

```# Step 4: Display the scores
print("Total Scores:")
print(" - Gryffindor:" + str(gryffindor))
print(" - Ravenclaw:" + str(ravenclaw))
print(" - Hufflepuff:" + str(hufflepuff))
print(" - Slytherin:" + str(slytherin))

```

#### Step 5: Selecting the house with the highest score

FlowchartPython Code
Python Code

```# Step 5: Select the house with the highest score
if gryffindor>=ravenclaw and gryffindor>=hufflepuff and gryffindor>=slytherin:
print("You belong to Gryffindor!")
elif ravenclaw>=gryffindor and ravenclaw>=hufflepuff and ravenclaw>=slytherin:
print("You belong to Ravenclaw!")
elif hufflepuff>=ravenclaw and hufflepuff>=gryffindor and hufflepuff>=slytherin:
print("You belong to Hufflepuff!")
elif slytherin>=ravenclaw and slytherin>=hufflepuff and slytherin>=gryffindor:
print("You belong to Slytherin!")
```

Can you think of any additional questions you could add to your program to take more accurate decisions?

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:

Did you like this challenge?

Click on a star to rate it!

Average rating 3.2 / 5. Vote count: 93

No votes so far! Be the first to rate this post.

As you found this challenge interesting...