库存帮助 Python

Inventory help Python

因此,如果玩家 may/not 的物品栏中有某些物品,我正在尝试安排我的文本基础游戏的游戏玩法将如何运作。

 print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
    if "comb" and "razor" in inventory:
        print ("and the table with the empty bottle.")
    else "comb" not in inventory and "razor" in inventory:
        print ("and the table with the empty bottle and comb.")
    else "razor" not in inventory and "comb" in inventory:
        print ("and the table with the empty bottle and razor")

提示我这行代码有语法错误

else "comb" not in inventory and "razor" in inventory:

我似乎看不出自己犯了什么错误,我是初学者,所以也许有其他方法可以满足我的需求。

print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
    if "comb" and "razor" in inventory:
        print ("and the table with the empty bottle.")
    elif "comb" not in inventory and "razor" in inventory:
        print ("and the table with the empty bottle and comb.")
    elif "razor" not in inventory and "comb" in inventory:
        print ("and the table with the empty bottle and razor")

你快到了

else 只能这样工作

else:
    do something

所以你的代码应该是这样的

 print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
    if "comb" and "razor" in inventory:
        print ("and the table with the empty bottle.")
    elif "comb" not in inventory and "razor" in inventory:
        print ("and the table with the empty bottle and comb.")
    elif "razor" not in inventory and "comb" in inventory:
        print ("and the table with the empty bottle and razor")

或者那个

 print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
    if "comb" and "razor" in inventory:
        print ("and the table with the empty bottle.")
    elif "comb" not in inventory and "razor" in inventory:
        print ("and the table with the empty bottle and comb.")
    else: #using the else here
        print ("and the table with the empty bottle and razor")

但是,在测试您的代码时,我意识到您放置逻辑的方式将无法正常工作。

使用 if all(x in inventory for x in ['comb','razor']) 将正确处理 inventorycombrazor 这两个变量的存在,并允许其他条件在如果缺少其他值之一,则采用正确的方式。

inventory = ['comb','razor']
#inventory = ['razor','']
#inventory = ['comb']

print("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up ju
mpsuit")
if all(x in inventory for x in ['comb','razor']):
    print ("and the table with the empty bottle.")
elif ('comb' not in inventory) and ('razor' in inventory):
    print("and the table with the empty bottle and comb.")
elif ('razor' not in inventory) and ('comb' in inventory):
    print("and the table with the empty bottle and razor")