fre1228 2022-11-18 11:45 采纳率: 83.3%
浏览 8
已结题

Python代码,识别动物明明输入了黑色条纹的条件依然识别为金钱豹,其他的也是这样,问一下是怎么回事

Python代码,识别动物明明输入了黑色条纹的条件依然识别为金钱豹,其他的也是这样,问一下是怎么回事


fp1 = open("features.txt", encoding='UTF-8')
lines1 = fp1.readlines()
leng = len(lines1)
 
 
def rules():
    test = 1
    list3 = list2.copy()
    while test == 1:
        test = 0
        if ("有毛发" in list3 or "有奶" in list3) and "哺乳动物" not in list3:
            print("使用规则:有毛发 or 有奶-->哺乳动物")
            test = 1
            list3.append("哺乳动物")
            print("当前动态数据库:", list3)
        elif "有羽毛" in list3 and "鸟" not in list3:
            print("使用规则:有羽毛-->鸟")
            test = 1
            list3.append("鸟")
            print("当前动态数据库:", list3)
        elif "会飞" in list3 and "会下蛋" in list3 and "鸟" not in list3:
            print("使用规则:会飞 and 会下蛋 --> 鸟")
            test = 1
            list3.append("鸟")
            print("当前动态数据库:", list3)
        elif "吃肉" in list3 and "食肉动物" not in list3:
            print("使用规则:吃肉-->食肉动物")
            test = 1
            list3.append("食肉动物")
            print("当前动态数据库:", list3)
        elif "有犬齿" in list3 and "有爪" in list3 and "眼盯前方" in list3 and "食肉动物" not in list3:
            print("使用规则:有犬齿 and 有爪 and 眼盯前方-->食肉动物")
            test = 1
            list3.append("食肉动物")
            print("当前动态数据库:", list3)
        elif "哺乳动物" and "有蹄" in list3 and "蹄类动物" not in list3:
            print("使用规则:哺乳动物 and 有蹄-->蹄类动物")
            test = 1
            list3.append("蹄类动物")
            print("当前动态数据库:", list3)
        elif "哺乳动物" and "反刍动物" in list3 and "反刍动物" not in list3:
            print("使用规则:哺乳动物 and 反刍动物-->蹄类动物")
            test = 1
            list3.append("蹄类动物")
            print("当前动态数据库:", list3)
        elif "哺乳动物" in list3 and "食肉动物" in list3 and "黄褐色" in list3 and "暗斑点" in list3:
            print("使用规则:哺乳动物 and 食肉动物 and 黄褐色 and 暗斑点 -->金钱豹")
            list3.append("金钱豹")
            test = 1
            print("当前动态数据库:", list3)
            return list3[-1]
        elif "哺乳动物" in list3 and "食肉动物" in list3 and "黄褐色" in list3 and "黑色条纹" in list3:
            print("使用规则:哺乳动物 and 食肉动物 and 黄褐色 and 黑色条纹 -->虎")
            list3.append("虎")
            test = 1
            print("当前动态数据库:", list3)
            return list3[-1]
        elif "蹄类动物" in list3 and "长脖子" in list3 and "长腿" in list3 and "暗斑点" in list3:
            print("使用规则:蹄类动物 and 长脖子 and 长腿 and 暗斑点 -->长颈鹿")
            list3.append("长颈鹿")
            test = 1
            print("当前动态数据库:", list3)
            return list3[-1]
        elif "蹄类动物"in list3 and "黑色条纹" in list3:
            print("使用规则:蹄类动物 and 黑色条纹 -->斑马")
            list3.append("斑马")
            test = 1
            print("当前动态数据库:", list3)
            return list3[-1]
        elif "鸟"in list3 and "长脖子"in list3 and "长腿"in list3 and "不会飞"in list3 and "有黑白二色"in list3:
print("使用规则:鸟 and 长脖子 and 长腿 and 不会飞 and 有黑白二色 -->鸵鸟")
            list3.append("鸵鸟")
            test = 1
            print("当前动态数据库:", list3)
            return list3[-1]
        elif "鸟"in list3  and "会游泳"in list3 and "不会飞"in list3 and "有黑白二色" in list3:
            print("使用规则:鸟 and 会游泳  and 不会飞 and 有黑白二色 -->企鹅")
            list3.append("企鹅")
            test = 1
            print("当前动态数据库:", list3)
            return list3[-1]
        elif "鸟"in list3 and "善飞" in list3:
            print("使用规则:鸟 and 善飞 -->信天翁")
            list3.append("信天翁")
            test = 1
            print("当前动态数据库:", list3)
            return list3[-1]
        else:
            print("匹配错误")
            return 0
 
 
print("----------知识数据库如下----------")
i = 0
list1 = []
list2 = []
 
for line in lines1:
    line = line.strip('\n')
    list1.append(line)
    if i == leng - 1:
        break
    print("{}:{}".format(i, line))
    i += 1
 
choice1 = input("请输入已知知识序号并以空格隔开")
temp = choice1.split(" ")
print("选中的事

img

  • 写回答

2条回答 默认 最新

  • 爱晚乏客游 2022-11-18 14:47
    关注

    img


    你自己的截图啊,就到有斑点就停了,进入if i==13这个语句了,就不会进到下面的elif le

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月21日
  • 已采纳回答 11月21日
  • 创建了问题 11月18日

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图