我的学习AI存在问题 - 该程序旨在了解动物之间的差异,并在提示时添加一些新动物。问题在于,我刚刚发现程序无法区分某种形式的奇怪条件动物(在我的情况下,鸽子和翠鸟)。
当给予所述动物时,该程序意味着对每只动物都有一个独特的问题。例如,如果我说我的动物是一只鸽子,那么该计划会提出一个问题,那就是“它有羽毛吗?”#34;你可以收集其余的
我将我的代码和问题放在下面。任何帮助都将是一个巨大的+。
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):