首页 > 解决方案 > 我的 python 文本游戏中的制作系统出现错误(当您没有列表中的所有项目时,我试图让它打印一些东西)

问题描述

#I just have these up here for context
def ifhealthlessthan50():

    while health<=50:

        choice2 = input("YOU ARE LOW ON HEALTH WHAT WOULD YOU LIKE TO DO?\n> ")

        if choice2.lower() == 'move n':
            print(random.choice(biomes),  "IS WHERE YOU END UP")

        elif choice2.lower() == 'move s':
            print("pls ")
            print(random.choice(biomes), "IS WHERE YOU END UP")

        elif choice2.lower() == 'move e':
            print("are")
            print(random.choice(biomes), "IS WHERE YOU END UP")

        elif choice2.lower() == 'move w':
            print("gosh")
            print(random.choice(biomes), "IS WHERE YOU END UP")

        elif choice2.lower() == 'exit':
            print("NOW EXITING...")
            sys.exit()

        elif choice2.lower() == 'inventory':
            print(things)

        elif choice2.lower() == 'craft':
            craftinginput = input("WHAT WOULD YOU LIKE TO CRAFT?\n> ")
            if craftinginput.lower() == 'wpickaxe':
                things.remove('PLANKS')
                things.remove('PLANKS')
                things.remove('PLANKS')
                things.remove('STICKS')
                things.remove('STICKS')
            elif craftinginput.lower() == 'wsword':
                things.remove('PLANKS')
                things.remove('PLANKS')
                things.remove('STICKS')

            elif craftinginput.lower() == 'wshovel':
                if 'PLANKS' and 'STICKS' and 'STICKS' not in things:
                    print("YOU DON'T HAVE THE RIGHT MATERIALS!")

                elif 'PLANKS' and 'STICKS' and 'STICKS' in things:
                    things.remove('PLANKS')
                    things.remove('STICKS')
                    things.remove('STICKS')

                else:
                    print("YOU DON'T HAVE THE RIGHT/ENOUGH MATERIALS!")

            else:
                print("THAT'S NOT A CRAFTABLE ITEM!")



        else:
            print("dont work lmao")
            ifhealthlessthan50()
ifhealthlessthan50()

我正在努力做到这一点,所以当我没有它显示的所有材料时,你没有正确/足够的材料!但是,相反,我收到此错误,这只是功能之一(问题出在唯一的功能)我真的不确定如何解决此问题,我在这里查看了很多帖子。

ValueError: list.remove(x): x not in list

标签: pythonarrayslisttext

解决方案


我相信你有一个逻辑错误,这些行导致你的问题:

if 'PLANKS' and 'STICKS' and 'STICKS' not in things:

elif 'PLANKS' and 'STICKS' and 'STICKS' in things:

您需要检查每个项目是否单独存在。目前,您只检查最后一项是否在things中。考虑这个例子:

things = ['STICKS']
print('PLANKS' and 'STICKS' in things)  # True

'PLANKS' 本身的计算结果为 True,因此仅在things数组中检查最后一个字符串。多次检查一个项目是否在数组中是另一个问题;我建议将您的things数组转换为字典。


推荐阅读