How to Create a Simple Menu Maker for Python Scripts

The aim of this page📝 is to share a module I’m using for creating a simple menu in Python scripts.

Pavol Kutaj
1 min readMar 8, 2023

Let’s see the CIA (code-in-action:) first

A simple menu asking for the number of menu item as a selection mechanism

First, put the following code into a dedicated module

  • It’s just a class that creates an object with list of menu items and has a printer method and a simple selector

Then, in your script, import it ⟹ initialize with a list of options ⟹ print_menu ⟹ and select_menu_item

  • Import your menu_maker (not going into the weeds of importing here)
  • It takes a list of items, for example
  • The select_menu_item() maps the numerical input to the proper name of the menu item — Delete is 1 ; Patch is 2 ; etc.
  • And that’s all… in my case, I continue with your selection whatever you please, e.g. I’m calling dedicated functions with readable code
  • Since it takes a list, you can dynamically generate menu items, too (like I am using a base name of files for certain menu items ⟹ you can map them to template files in your folder, etc.)

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com