I have been learning to make AI in python. I used the random module and the append command to give unknown input values. this is my code:
import random
greetings=["Hi there!","Sup!"]
goodbye=["have a nice day","byee!"]
keywords=["maths","swimming"]
responses=["one of the most interesting subjects","The best sport in the world"]
print(random.choice(greetings))
user=input("Chat with me(type 'bye' to quit) -> ")
user=user.lower()
keyword_found=False
while(user!="bye"):
for i in range(len(keywords)):
if(keywords[i] in user):
print("Bot: " responses[i])
keyword_found==True
if(keyword_found==False):
new_keyword=input("Couldn't get ya. pls tell me the name of the topic which you are talking about : ")
keywords.append(new_keyword)
new_response=input("Pls tell me about it : ")
responses.append(new_response)
user=input("Chat with me(type 'bye' to quit) -> ")
user=user.lower()
The program is working fine with known inputs. But when i am using the append, the "Couldn't get ya" is popping up even after appending the new input. See the attached picture to understand the scenario.
Please help me to fix it. I would be really greatful
CodePudding user response:
Error in code at line 15 keyword_found==True
, where you are not assigning variable but rather checking if True
Working code with minimum modifications:
import random
greetings=["Hi there!","Sup!"]
goodbye=["have a nice day","byee!"]
keywords=["maths","swimming"]
responses=["one of the most interesting subjects","The best sport in the world"]
print(random.choice(greetings))
user=input("Chat with me(type 'bye' to quit) -> ")
user=user.lower()
keyword_found=False
while(user!="bye"):
for i, keyword in enumerate(keywords):
if keyword in user:
print("Bot: " responses[i])
keyword_found=True
break
if not keyword_found:
new_keyword=input("Couldn't get ya. pls tell me the name of the topic which you are talking about : ")
keywords.append(new_keyword)
new_response=input("Pls tell me about it : ")
responses.append(new_response)
user=input("Chat with me(type 'bye' to quit) -> ")
user=user.lower()
CodePudding user response:
In
for i in range(len(keywords)):
if(keywords[i] in user):
print("Bot: " responses[i])
keyword_found==True
The line
keyword_found==True
is for the equality operator, while you want to use the assignment operator:
keyword_found=True
CodePudding user response:
Made 3 changes and it should work fine:
for i in range(len(keywords)):
if (keywords[i] in user):
print("Bot: " responses[i])
keyword_found = True # 1
break # 2
if (keyword_found == False):
new_keyword = input("Couldn't get ya. pls tell me the name of the topic which you are talking about : ")
keywords.append(new_keyword)
new_response = input("Pls tell me about it : ")
responses.append(new_response)
keyword_found = False # 3
user = input("Chat with me(type 'bye' to quit) -> ")
user = user.lower()