1

I'm trying to build an AI you teach and you ask questions and if it doesn't know it will ask you and puts an answer to go with it, I just cant get it to have multiple answers to one question.

I've kinda tried nested lists but choosing one randomly comes difficult and i don't think it was nested right.

quest=[]
ans=[]
loop=0
newword=['Oh, I dont know this!','Hmm Ive never heard of that.','idk','owo whats this?','Im still learning.']
running=True
while running==True:
  a=input('Ask a question')
  if a in quest:
    while loop<=len(quest):
      if a==quest[loop]:
        print(ans[loop])
        break
      else:
        loop=loop+1
  else:
    quest.append(a)
    b=input(newword[randint(0,4)]+' You tell me, '+a)
    ans.append(b)

'b' is answer given from a question to the user. In the if statement above it just checks if the question asked is already in a list. 'a' represents the question asked.

If i ask 'How are you doing?' it adds it to a question list and asks me in return. with time it i want it to have more than one answer to this question. so it will randomly pick from either good, or bad, ect... i have the questions in a loop. i just need to pair multiple answers with each question.

1
  • I can probably give you a useful class, namedtuple, or dict to solve this but I would need a better example. Please provide a few inputs and the output you expect for those inputs. Commented Apr 17, 2019 at 14:45

2 Answers 2

1

I might not be understanding the question here, but would something like a dictionary not work?

dict = {}
dict["your question or reference to your question number"] = []
dict["your question or reference to your question number"].append(b)
0
0

What you need to do is to return the question to the human, regardless of whether the AI knows an answer to it. If it doesn't, it will learn its first answer. It it does know already, it will append a new answer.

import numpy as np

known_questions_and_answers = {}
reactions_to_a_new_question = ["Oh, I don't know this!", "Hmm I've never heard of that.", "idk", "owo what's this?", "I'm still learning."]

while True:
    question = input('Ask a question')

    if question in known_questions_and_answers.keys():
        print(np.random.choice(known_questions_and_answers[question])) # Print one answer, randomly chosen in a list of valid answers.
    else:
        print(np.random.choice(reactions_to_a_new_question)) # React to an unknown question.
        known_questions_and_answers[question] = []

    answer = input(f"You tell me, {question}?") # Return the question.
    known_questions_and_answers[question].append(answer) # Append new question-answer pair.

Not the answer you're looking for? Browse other questions tagged or ask your own question.