Learning Python The Harry Potter Way!
- 5 days ago
- 6 min read

Harry Potter is one of my favourite novel characters, a character that inspired me to start writing stories of my own. But today, I am not going to talk about those stories but my own story of learning.
My teachers often complain that I do not listen to them, do not do classwork, avoid homework and most times, I am distracted. As per my mom, my laptop is the curse I have which is keeping me away from the real world!
I wonder if it is a curse?! Because I enjoy my time with it because not just I can write stories with it, I can also create things - things like graphics, animations, and even write codes. Do you know I love coding? And this love made me try creating a new coding language for myself - I call it YajatScript and it even has an interpreter. Yes, the base of it is HTML and Python....now, coming to Python.
My mom quickly opened a presentation on Python asking me how much of it I already know, need to revise or learn more. Well, I knew most of it already, at least 95% of it but my mom would not be satisfied without checking so she asked me to write the code given in the assignment quesiton in the course.
And what I did...just as expected...did not write that code but instead I wrote something else...code for a game to guess a character from Harry Potter series. At first my mom wondered what I was doing but being a creative person herself, she quickly appreciated and then, challenged me to write a game - a game that not just asked the user to guess the name but also threw multiple hints upon failure to answer, add differential scores (well, this was added later) for correct answers such that correct answer in the first attempt would get a higher score, and she asked me not just to run the code in Python but create a real HTML interface to show a good interface online like in a game.

Well, I could quickly produce the code and even knew how to write an HTML code but connecting them needed me to use an Integrated Development Environment (IDE) like VS Code by Microsoft. Well, I remembered using it once but I was not aware of how to run a Python code on it and then, on using AI, we discovered something called Flask, a library that when included allows us to run Python programs on VS Code. A file structure was suggested that was to be followed to make this run.
This was my first new lesson of the day...
LESSON#1: Its not just the code but also the file structure that has to be in place if your code has to reach a browser. Turned out flask understands only a specific structure and a flow in which it functions:
Browser
↓
HTML Form
↓
Flask Server
↓
Python Logic
↓
Game Data Dictionary
↓
Return HTML Page
So, I had to create these files - Python (Game code file), HTML (INdex file), CSS (To Style the appearance of the output) but not randomly - it had to follow this structure:
harry-potter-game
│
├── app.py
├── templates
│ └── index.html
│
└── static
└── style.css
As I continued, I found errors. Mom said these are called bugs (well, I already knew that) but what I was not aware was that bug fixing takes much more time than coding. Phew! But then, this was also an opportunity to learn - learn what can make or break a code.
LESSON#2: Missing a comma, one less space, wrong indentation, capitalization - turns out Python is a language that worries a lot about formats and punctuations like a typical English teacher, one mistake and you are penalized with all those red marks that decare your game is invalid because it is missing bla bla bla...So, be careful and remember these points when you are coding in python
Indentation is used to identify what comes inside a function. While this looks easier as you do not have to rely on those flowery brackers like Javascript, it also makes it confusing because you have to leave exactly 4 spaces to create this indentation
Don't forget to add ":" before you start adding the code within a function
You still need flowery brackets {} for defining custom datasets. But make sure, you do not miss the comma
"1": {
"hints": [
"Character is in Gryffindor house.",
"Character is brave.",
"Character is one of the main trio.",
"Character is the MC."
],
"correct": ["harry", "harry potter", "harry james potter"],
"help": "Look at the name of the series."
},
"2": {
"hints": [
"Character is in Gryffindor house.",
"Character likes food.",
"Character is one of the main trio.",
"Character is a foodie."
]
}
It is easy to miss that little comma so I colored it for you...
Be careful with variables, if you want to use any variable throughout your program, you need to make it global. So, just after you define your function, add the global variables.
def game():
global current_character
global score
global help_used
This means that you can't just start coding in Python without carrying the logic of your program in your mind and ideally on paper.
If I needed multiple characters, I needed a variable to store the selected character name or number for representation (current_character).
If I needed to give players a score, I needed a variable to save it (score)
If I had to do the variable scoring such as less score for utilizing more hints and more score for utilizing no hints, I needed a variable to save this too (help_used)
No wonder, variables is a huge chapter in Python. When I started learning, it sounded easy and boring but now, I knew how valuable and usable it is.
Let us come to my last lesson of the day...
Lesson#3: Well, a part of it I already knew - we have to first import all the libraries we need before starting to code such as random (because I wanted hints to be thrown to the user at random) but If I wanted my program to not just appear as text on Jupyter notebook but on the browser as a form, I needed to import flask which is a web framework for Python which can be used to build web applications and this framework is exactly how I was able to connect Python with CSS and HTML and run it on the browser through terminal. And for that all I had to do was open my Python file through this simple terminal command: Python app.py However, flask import was not as simple as import random but it need a proper syntax:
from flask import Flask, render_template, request.
Well, this learning journey sounds interesting when I do not have to learn the syntax just becuase the syntax is there to learn but because I understand why it is in place and what could go wrong when it is not used properly. In short, it is learning by doing, and the best part is not doing that random coding that I am not interested in (like input 2 numbers in 2 variables and check which is greater)...why would I do that? And how would checking which one is greater help me write applications (I wonder) but Guessing the Harry Potter Character! Well, this also required me to create variabels, input numbers to variables and run the checks to give a specific output. Like if the gamer used help or not (check), give the score less or more. But this did not sound boring as before.
So, here is my lesson for you who is reading...if you are learning coding, try using it to create something you really enjoy rather than just following the boring exercises and then, you will begin to enjoy coding and will not feel baffled by lectures.
Soon, I will be coming up with some more lessons on Python but these lessons will not be typical classroom notes. They will be in my style and in my stories. So, if you want to learn more from me, keep following this website and I will keep sharing....Until next communion, Lights Out!



Comments