0

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()
5
  • Use or instead of and if you want to run the loop while any of them are False Commented May 8, 2016 at 13:05
  • I need it to stop when they are all True Commented May 8, 2016 at 13:06
  • 2
    This is not a Python question. It's not even a programming question - it's a logic question. Think more carefully. If you need a hint, see if mathworld.wolfram.com/deMorgansLaws.html helps. Commented May 8, 2016 at 13:06
  • @Thomas: "All True" is the same thing as not "any False" Commented May 8, 2016 at 13:07
  • Thanks for pushing me to the right direction. I got this to work: while not(row1check == row2check == row3check == True): Commented May 8, 2016 at 13:11

4 Answers 4

1

You can use all function for this:

while not all([row1check, row2check, row3check]):

which will stop only when all of them are True.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.