Začněte nyníZačněte zdarma

Přidání rozmanitosti

Pořád stejné odpovědi mohou časem nudit. V tomto cvičení přidáš trochu variability. Když se zeptáš svého bota, jak se cítí, měla by být stejná pravděpodobnost, že odpoví "oh I'm great!" nebo "I'm very sad today".

Použiješ modul random – konkrétně random.choice(ls) – který náhodně vybere prvek ze seznamu ls.

Slovar responses, který přiřazuje každé zprávě seznam možných odpovědí, je už připravený.

Toto cvičení je součástí kurzu

Building Chatbots in Python

Zobrazit kurz

Interaktivní cvičení na vyzkoušení si v praxi

Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.

# Import the random module
____

name = "Greg"
weather = "cloudy"

# Define a dictionary containing a list of responses for each message
responses = {
  "what's your name?": [
      "my name is {0}".format(name),
      "they call me {0}".format(name),
      "I go by {0}".format(name)
   ],
  "what's today's weather?": [
      "the weather is {0}".format(weather),
      "it's {0} today".format(weather)
    ],
  "default": ["default message"]
}

# Use random.choice() to choose a matching response
def respond(message):
    # Check if the message is in the responses
    if message in responses:
        # Return a random matching response
        bot_message = ____(____[____])
    else:
        # Return a random "default" response
        bot_message = ____.____(____["____"])
    return bot_message
Upravit a spustit kód