Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

like image 943
Gleyak Avatar asked Jan 18 '26 15:01

Gleyak


1 Answers

Sure, you could use any for this. This should be equivalent.

if any(self[by1,bx1+x]=='A' for x in range(4)):
like image 95
Morgan Thrapp Avatar answered Jan 21 '26 04:01

Morgan Thrapp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!