1

I want to make a program that remembers something and can show it later on - like A.I.

First, I will show the program the question, like "How are you today?" and I also teach it the answer "Fine , you?", something like that.

Second, when I ask the program "How are you today?" or "How are you?", it should know the answer.

So far, I have this:

print ("Salut ! Am nevoie de ajutorul tau sa invat cateva propozitii...")
print ("Crezi ca ma poti ajuta ?")
answer1 = input("da/nu")

if (answer1=="da"):
  print ("Bun , acum tu poti sa pui intrebarea si raspunsul")
  print ("Spre exemplu , Primul lucru la inceput de linie trebuie sa fie intrebarea urmata de *?* ")
  print ("Apoi , raspunsul pe care eu trebuie sa il dau.")
  print ("Exemplu : Intrebare= Ce faci ? Raspuns= Bine , mersi :D")
question= "asd"
while (question != "stop"):
  question = input("The question: ")
  answer = input("The answer= ")

What should I do to be able to store the question, the answer for the respective question, then, when I input something like "Password" or any specific word to be able to test if it knows to reply my questions?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Gusto Pop
  • 53
  • 2
  • 7
  • Will the program continue to run? If so, why not just store questions and answers in a variable, e.g. a dictionary, and retrieve the answers from there? – Dux Jun 03 '18 at 20:50

2 Answers2

3

Try a dictionary data structure. This structure allows fast retrieval of a value (answer) given a key (question). Here's a sample program and output:

# dictionary to store the question-answer pairs
qa = {}

# store a series of question/answer pairs
while 1:
    question = input("add a question (or q to quit): ")

    if question == "q": 
        break

    answer = input("add the answer: ")
    qa[question] = answer

print("...")

# run the quiz
while 1:
    question = input("ask me a question (or q to quit): ")

    if question == "q": 
        break
    elif question in qa:
        print(qa[question])
    else:
        print("i'm not sure...")

Sample run:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

add a question (or q to quit):  what is the meaning of life?
add the answer:  42
add a question (or q to quit):  what is my password?
add the answer:  1234
add a question (or q to quit):  q
...
ask me a question (or q to quit):  what is the meaning of life?
42
ask me a question (or q to quit):  what is my password?
1234
ask me a question (or q to quit):  help?
i'm not sure...
ask me a question (or q to quit):  q

If you need to persist these questions beyond the program's run, write your qa dict to a file or store the questions and answers in a database such as SQLite.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Thanks . I have one more question . How do i store them to work in another session ? Not teach it every time ? – Gusto Pop Jun 03 '18 at 21:14
  • You can pickle the dictionary and write it to a file, then retrieve it from file on each program run: https://stackoverflow.com/questions/19201290/how-to-save-a-dictionary-to-a-file – ggorlen Jun 03 '18 at 21:23
0

You can use dictionaries.

answers = dict()
# usage is answers[question] = answer;
answers['How are you?'] = 'Good'
question = input()
if question in answers:
 print(answers[question])
DecaK
  • 286
  • 2
  • 13