mercredi 11 novembre 2015

Simplify a if statement Python

Is there a way to simplify this if-statement:

if self[by1,bx1]=='A' or self[by1,bx1+1]=='A' or self[by1,bx1+2]=='A' or self[by1,bx1+3]=='A':

coming from a class where self[y,x] fetch a data in a table.

The original code is:

for i in range(4):
                if self[by1,bx1]=='A' or self[by1,bx1+1]=='A' or self[by1,bx1+2]=='A' or self[by1,bx1+3]=='A':
                    print('There is already a ship here')
                    by1=0
                    bx1=0
                    self.placing_Battleship_p1()
                elif by1==0 or by1==0:
                    pass
                else:
                    self[by1,bx1+i]='B'

I want it to check if every position of my table are not equal to 'A' before changing them for a 'B'.

Aucun commentaire:

Enregistrer un commentaire