I am building a chatbot in python. I am having trouble running the code

Question:

import random
userKeywords = {"hi","hello","wassup","what'sup","greetings","sup","henlo","que onda","hola","hey","waddup"}

machineResponses = {"hello", "Hello there, I am a bot", "greetings from inside this computer"}

def machineAnswer(message):
    for key in userKeywords:
        if key == message:
            return random.choice(machineResponses)

def respondTo(message):
    print(machineAnswer(message))
respondTo("hello")

I am building a chatbot in python. I am having trouble running the code. My goal is to create a function that searches an array for a greeting keyword.If the keyword exists within the array, the bot responds with a similar response. For example, if user inputs “hello”, the bot must recognize that hello is one of the greeting keywords and print out a similar string to “hello” as a response, by randomly choosing a response from “machineResponses”. I have received the following errors:

print(machineAnswer(message))
File "C:Usersgilbeeclipse-workspacepython3.6BeginnerFilesChatBot", line 9, in machineAnswer
return random.choice(machineResponses)

File "C:UsersgilbeAppDataLocalProgramsPythonPython36-32librandom.py", line 259, in choice
return seq[i]

TypeError: 'set' object does not support indexing
Asked By: g24

||

Answers:

You can reduce the iteration and check.
The problem with your statement was random.choice does not support set object.

import random
userKeywords = {"hi","hello","wassup","what'sup","greetings","sup","henlo","que onda","hola","hey","waddup"}

machineResponses = list({"hello", "Hello there, I am a bot", "greetings from inside this computer"})

def machineAnswer(message):
    if message in userKeywords:
        return random.choice(machineResponses)

def respondTo(message):
    print(machineAnswer(message))
respondTo("hello")
Answered By: raviadhikari

Random.choice takes a random index from the object but you are using the set which doesn’t support indexing, You can convert your set into a list and use it

A set is just an unordered collection of unique elements. So, an
element is either in a set or it isn’t. This means that no element in
a set has an index.

import random
userKeywords = {"hi","hello","wassup","what'sup","greetings","sup","henlo","que onda","hola","hey","waddup"}

machineResponses = ["hello", "Hello there, I am a bot", "greetings from inside this computer"]

def machineAnswer(message):
    for key in userKeywords:
        if key == message:
            return random.choice(machineResponses)

def respondTo(message):
    print(machineAnswer(message))
respondTo("hello")

Output:

Hello there, I am a bot
Answered By: Aaditya Ura

I’d try this:

import random
userKeywords = ["hi","hello","wassup","what'sup","greetings","sup","henlo","que onda","hola","hey","waddup"]

machineResponses = ["hello", "Hello there, I am a bot", "greetings from inside this computer"]

def machineAnswer(message):
    if message in userKeywords:
        return machineResponses[random.randint(0, 2)]

def respondTo(message):
    print(machineAnswer(message))
respondTo("hello")

It worked when I tried it.

Answered By: spargle
import random
userKeywords = {"hi","hello","wassup","what'sup","greetings","sup","henlo","que onda","hola","hey","waddup"}

machineResponses = list({"hello", "Hello there, I am a bot", "greetings from inside this computer"})

def machineAnswer(message):
    if message in userKeywords:
        return random.choice(machineResponses)

def respondTo(message):
    print(machineAnswer(message))
respondTo("hello")
Answered By: Meghashyam Royal