chat gpt

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.8 kB
1
Indexable
Never
import random
import datetime

def greet():
    greetings = ["Hello! How can I assist you today?",
                 "Hi there! How may I help you?",
                 "Greetings! What can I do for you?"]
    return random.choice(greetings)

def calculate(expression):
    try:
        result = eval(expression)
        return f"The result is: {result}"
    except Exception as e:
        return f"Sorry, I couldn't calculate that. Error: {str(e)}"

def get_info(topic):
    if topic == "weather":
        return "The current weather is sunny and 75°F."
    elif topic == "time":
        current_time = datetime.datetime.now().strftime("%I:%M %p")
        return f"The current time is {current_time}."
    elif topic == "news":
        return "Here are the latest headlines: ..."
    else:
        return "I'm sorry, I don't have information on that topic."

def search_web(query):
    # Placeholder for searching the web based on the query
    return f"Search results for '{query}' are not available right now."

def assistant():
    print(greet())
    
    while True:
        user_input = input("> ")
        
        if user_input.lower() == "exit":
            print("Goodbye! Have a great day!")
            break
        elif user_input.lower().startswith("calculate"):
            expression = user_input.split(" ", 1)[1]
            print(calculate(expression))
        elif user_input.lower().startswith("get info"):
            topic = user_input.split(" ", 2)[2]
            print(get_info(topic))
        elif user_input.lower().startswith("search"):
            query = user_input.split(" ", 1)[1]
            print(search_web(query))
        else:
            print("I'm sorry, I didn't understand that. Please try again.")

assistant()
Leave a Comment