PYTHON PROJECT 15

Mini Python Assistant

Learn how to create a simple rule-based assistant using Python conditions, functions, strings, lists and user input.

1. Project Introduction

A Mini Python Assistant is a beginner-friendly project that responds to simple commands entered by the user.

The assistant can recognize commands such as greetings, help, time, date and exit.

This project is a rule-based educational assistant. It does not use an external AI API or machine-learning model.

2. Taking User Input

The input() function allows the user to communicate with our assistant.

message = input(
    "You: "
)

print(
    "You said:",
    message
)

3. Converting Text to Lowercase

Users may type commands using uppercase or lowercase letters. Using lower() makes command matching easier.

message = input(
    "You: "
).lower()

4. Creating a Greeting

We can use an if statement to detect a greeting.

if message == "hello":

    print(
        "Assistant: Hello!"
    )

5. Supporting Multiple Greetings

The in operator can check whether a value exists inside a list.

greetings = [
    "hello",
    "hi",
    "hey"
]

if message in greetings:

    print(
        "Assistant: Hello! How can I help?"
    )

6. Creating a Help Command

if message == "help":

    print(
        "Available commands:"
    )

    print("hello")
    print("time")
    print("date")
    print("help")
    print("exit")

7. Using the datetime Module

Python's datetime module can provide the current date and time.

from datetime import datetime

now = datetime.now()

print(now)

8. Creating a Time Command

from datetime import datetime

if message == "time":

    current_time =
        datetime.now().strftime(
            "%H:%M:%S"
        )

    print(
        "Assistant:",
        current_time
    )

9. Creating a Date Command

if message == "date":

    current_date =
        datetime.now().strftime(
            "%d-%m-%Y"
        )

    print(
        "Assistant:",
        current_date
    )

10. Creating an Assistant Function

Functions allow us to keep the assistant's logic organized.

def respond(message):

    message = message.lower()

    if message == "hello":

        return "Hello! How can I help?"

    return "I don't understand that command."

11. Returning Different Responses

def respond(message):

    message = message.lower()

    if message in [
        "hello",
        "hi",
        "hey"
    ]:

        return "Hello!"

    elif message == "help":

        return "Type help to see commands."

    else:

        return "Unknown command."

12. Creating a Command Dictionary

A dictionary can store command descriptions or responses.

responses = {

    "hello":
        "Hello! How can I help?",

    "thanks":
        "You're welcome!",

    "bye":
        "Goodbye!"

}

13. Checking Dictionary Commands

if message in responses:

    print(
        responses[message]
    )

14. Adding the Exit Command

A while loop can keep the assistant running until the user enters an exit command.

while True:

    message = input(
        "You: "
    ).lower()

    if message == "exit":

        print(
            "Assistant: Goodbye!"
        )

        break

15. Creating the Main Assistant Loop

while True:

    message = input(
        "You: "
    ).strip().lower()


    if message == "exit":

        print(
            "Assistant: Goodbye!"
        )

        break


    response = respond(
        message
    )


    print(
        "Assistant:",
        response
    )

16. Handling Empty Input

if not message:

    print(
        "Assistant: Please type something."
    )

    continue

17. Handling Unknown Commands

The assistant should provide a useful response when it does not recognize a command.

return (
    "I don't understand that command. "
    "Type 'help' to see available commands."
)

18. Complete Assistant Function

from datetime import datetime


def respond(message):

    message =
        message.strip().lower()


    if message in [
        "hello",
        "hi",
        "hey"
    ]:

        return (
            "Hello! How can I help?"
        )


    elif message == "help":

        return (
            "Commands: hello, time, "
            "date, help, thanks, exit"
        )


    elif message == "time":

        return datetime.now().strftime(
            "%H:%M:%S"
        )


    elif message == "date":

        return datetime.now().strftime(
            "%d-%m-%Y"
        )


    elif message == "thanks":

        return "You're welcome!"


    else:

        return (
            "I don't understand that command."
        )

19. Running the Assistant

while True:

    message = input(
        "You: "
    )

    if message.lower() == "exit":

        print(
            "Assistant: Goodbye!"
        )

        break

    print(
        "Assistant:",
        respond(message)
    )

20. Complete Project Flow

01 Start Assistant
02 Take Input
03 Normalize Text
04 Check Command
05 Generate Response
06 Display Response
07 Repeat
08 Exit

21. Concepts You Practiced

22. Important Note

This project is a rule-based assistant created for learning Python programming concepts.

A production AI assistant would require additional technologies such as an AI model/API, conversation management, security, authentication and backend infrastructure.

23. Run Complete Project 🚀

The tutorial is complete. Now open the interactive Mini Python Assistant.

Run Complete Project