python - 我的 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
解决方案
我相信你有一个逻辑错误,这些行导致你的问题:
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数组转换为字典。
推荐阅读
- unicode - UnicodeEncodeError:“ascii”编解码器无法在位置 3 编码字符“\u30db”:序数不在范围内(128)
- python - 如何在类上调用 write()?
- esp8266 - esp8266 固件更新后不启动
- python-3.x - 在执行多处理池时跟踪字典中的值
- sql - 过滤表,以便不返回交换列重复的行
- configuration - Web.config 中无法识别的属性“requestPageSize”
- qt - 在 32 位 Qt5 应用程序中启用 LARGEADDRESSAWARE 是否安全?
- sql - 基于一列模式的 SAS/SQL 连接匹配另一列
- api - googleapi:错误 400:错误请求,失败前提条件
- tensorflow - 挤压一层的输出