python文本基础游戏和/或功能错误?

时间:2018-01-08 23:48:43

标签: python text-based

我正在制造一个监狱逃生,并且在一个特定的拼图中,玩家应该肢解一只手,用手肥皂,然后将其清洗以便通过另一个房间的扫描仪进入。玩家第一次选择肢解手时,在库存中将其添加为“血腥的手”,当角色使用肥皂清洁手时,我会更换血腥的手&# 39;用肥皂的手'然后在完全洗完之后,我用肥皂的手代替了#39;到手#39在库存中。我的问题是,无论我做什么,如果玩家决定不止一次地毁手,那么库存会不断添加“血腥的手”。尽管玩家已经在他们的库存中有这个。我想防止任何额外的血腥手'在玩家拥有“血腥手”的同时添加到广告资源中,'肥皂手'或者'手。我希望我能够彻底解释这一点,并且我可能会得到很好的反馈,可以帮助我解决这个错误。我已经尝试了(和)和(或)函数,但这两个函数都没有。这是我的代码:

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.")

3 个答案:

答案 0 :(得分:1)

您可以单独询问每个项目:

elif ("bloody hand" in inventory) or ("soapy hand" in inventory) or ("hand" in inventory):

答案 1 :(得分:0)

如果你想要有很多这样的谜题,我建议你做一个这样的功能:

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。它在行if is_inside(inventory, "razor")

中也有效

答案 2 :(得分:0)

您想要检查广告资源中的any项目是否属于set手牌类型:

hands = {"bloody hand", "soapy hand", "hand"}

if "bloody hand" not in inventory:
    ...
elif any(item in hands for item in inventory):
    ...