Reputation: 85
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()
Upvotes: 0
Views: 88
Reputation: 772
You also can use and
operator like this:
row1check = False
row2check = False
row3check = False
def x_turn():
global row1check
global row2check
global row3check
while not (row1check and row2check and row3check):
print('Looping...')
row1check = True
row2check = True
row3check = True
if __name__ == '__main__':
x_turn()
This while loop while stop when row1check, row2check, row3check are all True.
Upvotes: 0
Reputation: 2496
You can use all function for this:
while not all([row1check, row2check, row3check]):
which will stop only when all of them are True.
Upvotes: 1
Reputation: 15310
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
Upvotes: 0
Reputation: 10533
Your while loop syntax is right. But based on your requirements, you should be using:
while (row1check == False) or (row2check == False) or (row3check == False):
Using or
over here means it should continue running the code as long as any of these conditions is true. That is, while any of row1check, row2check, or row3check is false.
Upvotes: 0