ComenzarEmpieza gratis

Charla

Ahora vas a dejar atrás el simple EchoBot y crearás un bot capaz de responder preguntas básicas como "¿Cómo te llamas?" y "¿Qué tiempo hace hoy?"

Usarás un diccionario con estas preguntas como claves y las respuestas correctas como valores.

Esto significa que el bot solo responderá correctamente si el mensaje coincide exactamente, lo cual es una gran limitación. En ejercicios posteriores crearás soluciones mucho más robustas.

La función send_message() ya está definida para ti, al igual que las variables bot_template y user_template.

Este ejercicio forma parte del curso

Creación de chatbots en Python

Ver curso

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Define variables
name = "Greg"
weather = "cloudy"

# Define a dictionary with the predefined responses
responses = {
  "what's your name?": "my name is {0}".format(name),
  "what's today's weather?": "the weather is {0}".format(weather),
  "default": "default message"
}

# Return the matching response if there is one, default otherwise
def ____(____):
    # Check if the message is in the responses
    if ____ in ____:
        # Return the matching message
        bot_message = ____[____]
    else:
        # Return the "default" message
        bot_message = ____["____"]
    return bot_message
Editar y ejecutar código