Understanding inputMenu() from PyInputPlus with Examples

Comprehensive Guide to inputMenu() from PyInputPlus

inputMenu() is a user-friendly function from the PyInputPlus module that allows you to present menu-like options to users. This function is excellent for building interactive command-line interfaces where choices need to be constrained.


🔹 What is inputMenu()?

inputMenu() displays a list of choices and lets the user select one. The function ensures that only valid options are accepted, reducing the need for manual input validation.


📌 Basic Example

This basic example presents a numbered list of programming languages:

import pyinputplus as pyip

lang = pyip.inputMenu(['Python', 'Java', 'C++'], numbered=True)
print(f"Language selected: {lang}")

Explanation: The numbered=True option tells inputMenu() to display the choices with numbers. The user can type either the name or the number.

Note: The inputMenu()has default prompt Please select one of the following:. We will learn how to customise it later in this post

Sample Interaction 1: Choosing Number


Please select one of the following:
1. Python
2. Java
3. C++
java
Language selected: Java

Sample Interaction 2: Choosing Name

Please select one of the following:
1. Python
2. Java
3. C++
Python
Language selected: Python

Note:It is not Case Sensitive So both 'Python' and 'python' have same output

Example:

Please select one of the following:
1. Python
2. Java
3. C++
python
Language selected: python

📌 Custom Prompt

import pyinputplus as pyip

lang = pyip.inputMenu(['Python', 'Java', 'C++'], numbered=True,prompt = "Enter Your Choice: \n")
print(f"Language selected: {lang}")

Sample Interaction :


Enter Your Choice:
1. Python
2. Java
3. C++
java
Language selected: Java

📝 Bulleted Menu

If you prefer a bulleted list instead of numbers, use numbered=False:

It cannot Support numbers(1,2...) as Input

import pyinputplus as pyip
food = pyip.inputMenu(['Pizza', 'Burger', 'Pasta'], numbered=False)
print(f"Food chosen: {food}")
Please select one of the following:
* Python
* Java
* C++
Python
Language selected: Python

Explanation: This version shows options with * bullets. The user must type the name exactly unless caseSensitive is adjusted.


⛔ Limit Attempts and Provide Default Value

import pyinputplus as pyip

option = pyip.inputMenu(['Yes', 'No'], limit=2, default='No')
print(f"Final decision: {option}")

Explanation: The limit sets how many invalid attempts are allowed. If the user fails to provide a valid response in 2 tries, default='No' is returned automatically.

Sample Interaction:No Correct Input Is Given

Please select one of the following:
* Yes
* No
sorry
'sorry' is not a valid choice.
Please select one of the following:
* Yes
* No
idk
'idk' is not a valid choice.
Final decision: No

Sample Interaction:Correct Input Is Given

Please select one of the following:
* Yes
* No
yes
Final decision: Yes

🔤 Enable Case Sensitivity

Sometimes users may type options in different cases. You can handle this using caseSensitive=False.

import pyinputplus as pyip

choice = pyip.inputMenu(['Red', 'Blue'], caseSensitive=True)
print(f"Color: {choice}")

Explanation: This allows "red", "Red", or "RED" will be treated different.


📦 Real-World Example: Mode Selection

import pyinputplus as pyip

mode = pyip.inputMenu(['Basic', 'Advanced', 'Exit'], numbered=True, prompt="Select mode:\n")
print(f"Running in {mode} mode.")

Use Case: Great for terminal-based apps that switch between different functionalities or exit conditions.


🎯 Summary

  • Safe input: Users can only choose valid options.
  • Flexible display: Numbered or bulleted menus.
  • Custom prompts: Easily adjust user instructions.
  • Error handling: Use limit and default for better control.

Using inputMenu() makes your programs more user-friendly and error-resistant. Ideal for interactive Python scripts and beginner projects.

📚 Related Topics from PyInputPlus:


Previous Post Next Post

Contact Form