Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 程序可以';我不知道这两种动物有什么区别?_Python_Artificial Intelligence - Fatal编程技术网

Python 程序可以';我不知道这两种动物有什么区别?

Python 程序可以';我不知道这两种动物有什么区别?,python,artificial-intelligence,Python,Artificial Intelligence,我对我的学习人工智能有一个问题——该程序旨在学习动物之间的差异,并在提示时添加一些新的动物。问题是,我刚刚发现,程序无法区分某些形式的奇怪条件动物(在我的例子中,鸽子和翠鸟)之间的区别 该计划的目的是在给每只动物一个独特的问题时,给它一个独特的问题。例如,如果我说我的动物是一只鸽子,那么程序会问一个问题,“它有羽毛吗?”然后你可以收集剩下的 我将把我的代码和问题放在下面。任何帮助都将是巨大的+ animalDict = {"Does it swing from trees?":["monkey"

我对我的学习人工智能有一个问题——该程序旨在学习动物之间的差异,并在提示时添加一些新的动物。问题是,我刚刚发现,程序无法区分某些形式的奇怪条件动物(在我的例子中,鸽子和翠鸟)之间的区别

该计划的目的是在给每只动物一个独特的问题时,给它一个独特的问题。例如,如果我说我的动物是一只鸽子,那么程序会问一个问题,“它有羽毛吗?”然后你可以收集剩下的

我将把我的代码和问题放在下面。任何帮助都将是巨大的+

animalDict = {"Does it swing from trees?":["monkey"], "Does it purr?":["cat"], "Does it have a tail?":["cat","monkey"]}
foundattributes = list()
notpossessed = list()
finalattributes = list()
matches = list()
tempmatches = list()
finalmatches = list()
conflict = list()
def learn():
  print("Computer: I don't know that animal.")
  newAnimal = input("Computer: You were thinking of a(n): ").lower()
  newAttribute = input("Computer: Please input an attribute for this animal, in the form of a question.\n> ").capitalize()
  animalDict[newAttribute]=[newAnimal]
  for attribute in foundattributes:
    if attribute not in notpossessed:
      finalattributes.append(attribute)
  for attribute in finalattributes:
    animalDict[attribute].append(newAnimal)
while True:
  print("Computer: Think of an animal.")
  del foundattributes[:]
  del notpossessed[:]
  del matches[:]
  del conflict[:]
  del finalmatches[:]
  del finalattributes[:]
  for question in sorted(animalDict):
    del tempmatches[:]
    response=input("Computer: %s\n> " % question)
    if response.lower()=="yes":
      if question not in foundattributes:
        foundattributes.append(question)                                            
      for eachAnimal in animalDict[question]:
        if eachAnimal not in matches:
          matches.append(eachAnimal)
        if eachAnimal not in tempmatches:
          tempmatches.append(eachAnimal)                              
      for animal in matches:                                                           
        if animal not in tempmatches:
          matches.remove(animal)
          conflict.append(animal)
    elif response.lower()=="no":
      if question not in notpossessed:
        notpossessed.append(question)
      for animal in animalDict[question]:
        if animal not in conflict:
          conflict.append(animal)
    else:
      print("Computer: I don't understand '%s'"%response)

  for animal in matches:
    if animal not in conflict:
      finalmatches.append(animal)
  for clash in conflict:
    if clash in finalmatches:
      finalmatches.remove(clash)
  if len(finalmatches) == 1:
    conclusion=input("Computer: Is it a %s?\n> "%finalmatches[0])
    if conclusion.lower() == "yes":
      print("Solved!")
    elif conclusion.lower() == "no":
      learn()
    else:
      print("Computer: I don't understand '%s'"%response)
      break
  elif len(finalmatches) <1:
    learn()
  else:
    learn()

你应该把它分成不同的函数,而不是让它作为一个长脚本存在。如果在这样做之后,您能更好地理解您的程序及其缺陷,我也不会感到惊讶。。。(您的代码很难理解)
Computer: Think of an animal.
Computer: Does it have a tail?
> yes
Computer: Does it purr?
> no
Computer: Does it swing from trees?
> no
Computer: I don't know that animal.
Computer: You were thinking of a(n): pigeon
Computer: Please input an attribute for this animal, in the form of a question.
> does it have feathers?
Computer: Think of an animal.
Computer: Does it have a tail?
> yes
Computer: Does it have feathers?
> yes
Computer: Does it purr?
> no
Computer: Does it swing from trees?
> no
Computer: Is it a pigeon?
> no
Computer: I don't know that animal.
Computer: You were thinking of a(n): kingfisher
Computer: Please input an attribute for this animal, in the form of a question.
> does it eat fish?
Computer: Think of an animal.
Computer: Does it eat fish?
> yes
Computer: Does it have a tail?
> yes
Computer: Does it have feathers?
> yes
Computer: Does it purr?
> no
Computer: Does it swing from trees?
> no
Computer: I don't know that animal.
Computer: You were thinking of a(n):