Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop = False
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,224 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  While loop doesn't end when False Kanashi 2 2,564 Nov-21-2019, 02:38 AM
Last Post: Kanashi
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,945 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  Returning true or false in a for loop bbop1232012 3 8,124 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  Get True of false outside a loop morgandebray 2 2,448 Aug-09-2018, 12:39 PM
Last Post: morgandebray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020