我想做一个能记住一些东西并能在以后显示出来的程序--就像人工智能。
首先,我会向程序展示这个问题,比如“你今天好吗?”我还教它“很好,你呢?”之类的答案。
第二,当我问节目“你今天好吗?”或者“你好吗?”,它应该知道答案。
到目前为止,我有这样的想法:
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= ")
我应该怎么做才能存储问题,相应问题的答案,然后,当我输入“密码”或任何特定的单词时,才能测试它是否知道回答我的问题?
发布于 2018-06-04 05:01:19
尝试使用dictionary数据结构。此结构允许在给定关键字(问题)的情况下快速检索值(答案)。下面是一个示例程序和输出:
# 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...")
示例运行:
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
如果您需要在程序运行后保留这些问题,可以使用write your qa
dict to a file或将问题和答案存储在诸如SQLite之类的数据库中。
https://stackoverflow.com/questions/50670774
复制相似问题