I am building a Chatbot and I was able to make it answer to me randomly. I added all the responses in a list and whenever I greet it, It shoots me a random answer. Now this is fine, but sometimes, the program throws an exception - IndexError: list index out of range. I don't understand why the list is going out of range. The list has 6 items, and by using random.randint(0,len(slist)) I was able to get a random response.
I have used exception handling to fix this problem. But, I want to know why it gives an error.
Here's the code with out exception handling:
if self.Has_user_greeted == False: self.AI_Greet() else: # goes out of range here self.AI_respond = random.randint(0,len(self.AI_Greeted)) print(self.AI_Greeted[self.AI_respond])
Aucun commentaire:
Enregistrer un commentaire