简体   繁体   中英

How to create chatbot with predefined inputs and responses are given in python?

I have inputs and responses.

userList:["Hi","What is my commission?","yes"]
cbList: ["Hi Magiee,How can I help you?","Would you like to know your commission?","Can you help me with NPN number?"]

I tried like below:

import pandas as pd
data=pd.read_excel("path",sheet_name='Sheet2')
print(data)
userList=data["User"].tolist()
cbList=data["CB"].tolist()
while True:
    userInput=input("User:")
    if userInput in userList:
        print(cbList)
    else:
        print("Goodbye")

I am getting error: TypeError: 'str' object is not callable . I also tried with creating a dictionary with key and values guided by @Jammy.But I have same key with different values.It over written the last value with first value. Please,suggest me how can i create chatbot with predefined inputs and responses are given?

One option you can do is to create a dict of list like:

import random
responses = {'Hi!': ['Hello there', 'hi man']}
default_responses = ['I dont know', 'GoodBye']
while True:
    user_input = input('User:')
    if user_input == 'exit':
        break
    print(random.choice(responses.get(user_input, default_responses)))

Explenation:

random.choice choose a random value from a list

responses.get trys to get value of user_input from the dict, and if not found gets a default valuie of default_responses

Update

while True:
    user_input = input('User:')
    if user_input == "What is my commission?":
        user_input = input('Would you like to know your commission?')
        if user_input == 'yes':
             user_input = input('Can you help me with NPN number?')

and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM