你幾乎沒有
的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'])
會正確對待這兩個變量,在inventory
comb
和razor
的存在,並允許以正確的方式來推出的其他條件,如果其他價值的缺失。
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")
我想你是指'elif',而不是'else'。 – khelwood