Python Forum

Full Version: While loop = False
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am making a tic tac toe game and I set a while loop = game but when I call my check_X_Repeat function to make it false it does not do so I want to know why the function does not set it False when it meets the criteria, thanks!

def user_X():
    global counter
    counter += 1
    correct = True
    while True:
        while correct:
            try:
                show()
                user_X = input("'X' type a number: ")
                user_X = int(user_X)
                # Check for X
                check_X_Repeat(user_X)
            except ValueError:
                print("Type a number...")
            else :
                pass
        # X is placed
        board[user_X] = 'X'
        break
# Check

def check_X_Repeat(user_X):
    global correct
    if board[user_X] != 'X':
        correct = False
        return
    else :
        print("Try again..")
        return
    return
Since you have an assignment statement (correct = True) in your function user_X(), correct is a local variable to that function. You need to assign the value of correct in a global scope to make it global, and/or include correct in your global statement at the start of the user_X() function.

A better option might be to have check_X_Repeat() return a value of True or False, and assign that to your local variable in the user_X() function.