In order to learn how to code, most people will write a short text-based role-playing game (RPG). It is a great way to understand object oriented principles, like when your Sword
class inherits from your Weapon
class that inherits from your Item
class.
The programmer-to-be will lead their players through a short fantastic adventure, prompting them with question after question like "where do you want to go next" or "a huge rock blocks your path, what do you do".
However, these prompts usually rely on the user to manually type out their answers, which I always found to be a less-than-ideal user interface. I wanted to create neat multiple choice menus that present the users/players with the available options.
There is a Python package for everything
There is - of course - a package for this, called python-inquirer. With this package you can create really pretty terminal-based single- and multiple choice prompts. (You can even create your own themes.) What you get back however, is a dictionary with the selected values.
If you are coding an RPG though, you usually want to bind the user's choice to some sort of functionality, so unpacking a dict like {"next_move": "north"}
to actually execute the functionality you want when the player decided to go north, seems like an unnecessary extra hoop to jump through.
My first own package
This and a handful of other use cases are the reason I have built upon python-inquirer to create my first open-source package published on the Python Package Index, which, as a self-taught developer is a huge step for me and so I am absolutely thrilled to put it out there and (hopefully) have a lot of people use it and comment on it.
So here it is: python-inquirer-executor. The way it works is pretty simple: You create a list of functions, for example:
def go_north(): """North""" print("There is the wall, do you really dare go north of the wall?") def go_south(): """South""" print("It's really hot here, you start sweating.") def go_east(): """East""" print("They serve good food here.") def go_west(): """West""" print("Looks like you crashed an ogre wedding, everyone is really nice about it though.") list_of_functions = [go_north, go_south, go_east, go_west]
This list of functions will represent your player's choices and there is nothing more you have to do except to pose the question:
from inquirer_executor import InquirerExecutorList InquirerExecutorList("Where do you want to go?", list_of_functions).prompt_and_execute()
This produces a beautiful interface for the user to choose his option:
Terminal output after the player chose "West".
As you can see, there are no extra hoops, the action is as tightly coupled with the users choice as can be, neatly organised, because each function is represented by it's own docstring.
It's actually quite fun to use, so much so that I have built two example projects with it, where you can explore parts of the package.
I have put a lot of effort into writing the README and commenting the code both in the project and the examples project to make this accessible for as many people as possible. I would love if you gave it a whirl and dropped me a line of feedback.
I'm really excited to (hopefully) see other people use this, learn from and with it, and if you share your RPG that you made with it with me, I'll make sure to play it :) Until then, keep on learning.
Posted from my blog with SteemPress : http://blog.vomkonstant.in/2019/08/08/how-to-bind-user-choice-to-functionality-in-python-my-first-authored-open-source-package/