Hello friends Welcome to Gran💡Light. In this blog we will see Creating An Interactive Command Line Program In Python
Creating an Interactive Command Line Program in Python
Python is a powerful and flexible programming language. It's easy to learn and can be used to create a wide variety of applications, including interactive command line programs. This post will walk through the process of creating an interactive command line program in Python.
Setting Up the Program
First, we need to create a new python file. We can do this by opening up a text editor, such as Notepad, and saving the file with a .py extension. Now let's start writing some code. We'll start by importing the sys module, which will give us access to the command line arguments.
import sys
Next, let's create a function that will display a prompt for the user and then read their input. We'll call this function get_input():
def get_input():
# Print prompt
print("What would you like to do?")
# Read input from the command line
user_input = input()
# Return the user's input
return user_input
Now that we have our get_input() function set up, we can start creating our main loop. This is where all of the program's logic will go. We'll use a while loop to keep the program running until the user exits the program:
while True:
# Get the user's input
user_input = get_input()
# Exit the program if the user types "exit"
if user_input == "exit":
break
# Otherwise, process the user's input
# ...
Processing User Input
Now that the main loop is set up, we need to write some code to process the user's input. We can do this by using an if-else statement to check the user's input and then execute the appropriate code. For example, if the user inputs "add", we might want to prompt them for two numbers and then add them together.
# If the user inputs "add", prompt for numbers and add them
if user_input == "add":
# Prompt for numbers
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
# Add the numbers together
result = num1 + num2
print("The result is " + str(result))
# Else if the user inputs "subtract"...
elif user_input == "subtract":
# Prompt for numbers
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
# Subtract the numbers
result = num1 - num2
print("The result is " + str(result))
# Otherwise, print an error message
else:
print("Error: Unknown command")
Using the same pattern, we can add as many commands as we want. Once we're done, our program should look something like this:
import sys
def get_input():
print("What would you like to do?")
user_input = input()
return user_input
while True:
user_input = get_input()
if user_input == "exit":
break
elif user_input == "add":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
result = num1 + num2
print("The result is " + str(result))
elif user_input == "subtract":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
result = num1 - num2
print("The result is " + str(result))
else:
print("Error: Unknown command")
Conclusion
There are endless possibilities when it comes to creating command line programs in Python. All it takes is a little creativity and effort to make something really cool. With just a few lines of code, we’ve created an interactive program that can respond to user input.
For more information, visit Our blog.
Technology That You Don't Know