如何在列表中为变量提供自己的变量以从中随机选择

时间:2019-04-17 14:32:45

标签: python

我正在尝试构建一个您教给您的AI,然后您问问题,如果它不知道会问您并提出答案,我只是无法让它对一个问题有多个答案。

我已经尝试过嵌套列表,但是随机选择一个列表很困难,而且我认为嵌套列表不正确。

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'是从问题中向用户给出的答案。 在上面的if语句中,它只是检查所问的问题是否已经存在      列表。 'a'代表提出的问题。

如果我问“你好吗?”它将其添加到问题列表,并要求我返回。随着时间的流逝,我希望它对这个问题有多个答案。因此它会从好坏之间随机选择...我有一个循环的问题。我只需要将多个答案与每个问题配对即可。

2 个答案:

答案 0 :(得分:1)

我可能在这里不明白这个问题,但是像字典这样的东西不起作用吗?

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

答案 1 :(得分:0)

您需要做的是将问题返回给人类,而不管AI是否知道答案。如果没有,它将学习其第一个答案。它确实知道,它将附加一个新答案。

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.