2017-10-19 4 views
0

私は、自分の在庫にアイテムが含まれていてもいなくても、テキストベースゲームのゲームプレイがどのように機能するのかを調整しようとしています。それは私が私が私が作ったものを誤って見るように見えることはできませんコードインベントリヘルプPython

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

、私は初心者ですので、おそらくそこにあります私のニーズを実行する別の方法です。

+8

「else」ではなく「elif」を意味すると思います。 – khelwood

答えて

0

あなたはほとんどそこ

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

か、その

ようになります10

しかし、コードをテストするときに、ロジックを配置する方法が正しく機能しないことに気付きました。

if all(x in inventory for x in ['comb','razor'])inventoryで正しく二つの変数、combrazorの存在を治療し、他の値のいずれかが欠けていた場合、他の条件は正しい方法で展開することを可能にすることを使用。

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