我正在创建一个越狱游戏,在一个特定的益智游戏中,玩家应该残害一只手,用肥皂洗手,然后清洗它,以便通过不同房间的扫描仪进行访问。当玩家第一次选择伤手时,在清单中它被添加为“血手”,当角色使用肥皂洗手时,我将“血手”替换为“肥皂手”,然后在完全清洗后,我将清单中的“肥皂手”替换为“手”。我的问题是,无论我怎么做,如果玩家决定不止一次地残害手牌,库存就会继续增加“血手”,尽管玩家的库存中已经有了“血手”。当玩家有“血手”,“肥皂手”或“手”时,我想防止任何额外的“血手”添加到库存中。我希望我已经充分解释了这一点,并希望我能得到好的反馈,这些反馈可能会帮助我修复这个错误。我已经尝试过( and )和(or)函数,但这两个函数都不起作用。下面是我的代码:
elif choice == "use razor on hand":
print ("(cut up mutilated guard's hand)")
if "bloody hand" not in inventory:
if "razor" in inventory:
print ("Furrowing your eyebrows as cold sweat trickled down your neck, you took out your razor blade.")
print ("Slowly placing the fairly sharp tool on the mutilated guard’s wrist, you closed your eyes and felt")
print ("flesh of the guard start to tear. You finished the deed. The bloody hand was added to your")
print ("inventory.")
add_to_inventory("bloody hand")
print("")
print ("Inventory: " + str(inventory))
elif "razor" not in inventory:
print ("You don't own this item in your inventory.")
elif "bloody hand" or "soapy hand" or "hand" in inventory:
print ("You already own this item in your inventory.")
发布于 2018-01-09 07:53:48
你可以单独询问每一项:
elif ("bloody hand" in inventory) or ("soapy hand" in inventory) or ("hand" in inventory):
发布于 2018-01-09 08:01:36
如果你想有很多这样的拼图,我建议你做一个这样的函数:
def is_inside(inventory, item_2_check):
for item in inventory:
if item_2_check in item:
return True
else:
return False
if not is_inside(inventory, "hand"):
#Whatever you want to do.
这将返回True,表示为"soapy“、”soapy hand“和"hand”。它在if is_inside(inventory, "razor")
行中也是有效的
发布于 2018-01-09 08:12:59
您需要检查库存中的物料是否在手工类型的中:
hands = {"bloody hand", "soapy hand", "hand"}
if "bloody hand" not in inventory:
...
elif any(item in hands for item in inventory):
...
https://stackoverflow.com/questions/48159666
复制相似问题