你快到了
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']) 将正确处理两个变量的存在,comb 和 razor 在 inventory 中,如果缺少其他值之一,则允许以正确的方式推出其他条件.
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")