Thomas Thomas - 7 months ago 10
Python Question

Python while loop multiple checks

Good Morning,

This while loop stops as soon as one of the row1check variables turns True? I want the loop to keep looping until all 3 variables are True. I'm I using the loop correctly?

def x_turn ():
global row1check
global row2check
global row3check
while (row1check == False) and (row2check == False) and (row3check == False):
move = raw_input("Player X Enter coordinates 'row,col': ")

row = move[0]
column = move[2]

if row == "1" and column == "1" and row1[0] == " ":
row1[0] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
if row == "1" and column == "2" and row1[1] == " ":
row1[1] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
if row == "1" and column == "3" and row1[2] == " ":
row1[2] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
if row == "2" and column == "1" and row2[0] == " ":
row2[0] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
if row == "2" and column == "2" and row2[1] == " ":
row2[1] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
if row == "2" and column == "3" and row2[2] == " ":
row2[2] = "x"
draw_matrix()
check_game()
end_game()
o_turn()

Answer

Switch to or instead of and:

while (row1check == False) or (row2check == False) or (row3check == False):

This way only when all 3 conditions are True you will stop looping.

P.S you can also use all:

>>> data = [False, False, False]
>>> not all(data)
True