私は戦艦ゲームを作っています。このコードは、何隻のボートが残っているかを確認します。私はそれがループのために簡単に行うことができますが、何らかの理由でそれの周りに私の頭をラップすることはできません知っている。これはコードです:if文のこのチェーンを簡単に書く方法は?
#empty Dictonary. This Dict will store the coords of the ship
boatDict = {'destroyer1': [], 'destroyer2': [],
'submarine1': [], 'submarine2': [],
'battleship': [], 'carrier': []}
if boatDict['destroyer1'] == []:
destroyer = 2
elif boatDict['destroyer1'] != []:
destroyer = 1
elif boatDict['destroyer2'] != []:
destroyer = 0
if boatDict['submarine1'] == []:
submarine = 2
elif boatDict['submarine1'] != []:
submarine = 1
elif boatDict['submarine2'] != []:
submarine = 0
if boatDict['battleship'] == []:
battleship = 1
elif boatDict['battleship'] != []:
battleship = 0
if boatDict['carrier'] == []:
carrier = 1
elif boatDict['carrier'] != []:
carrier = 0
ありがとうございます!
EDIT:質問とコード明確
空でないときに、リストには何が入っていますか? –
コード全体を投稿してください。 – yash
三元表記がここでは良いアイデアとなるかどうかは不明です。 –