首页 > 解决方案 > 为什么即使满足条件,这个布尔值也不为真?

问题描述

print("Tic Tac Toe created by Pher")
row_a = ["□","□","□"]
row_b = ["□","□","□"]
row_c = ["□","□","□"]

def ResetGame():
    row_a = ["□","□","□"]
    row_b = ["□","□","□"]
    row_c = ["□","□","□"]

def DrawBoard():
    print()
    print("  0 1 2")
    print("A "+row_a[0]+" "+row_a[1]+" "+row_a[2])
    print("B "+row_b[0]+" "+row_b[1]+" "+row_b[2])
    print("C "+row_c[0]+" "+row_c[1]+" "+row_c[2])

def VictoryCheck():
    #checking for x wins
    #hortizontal checks
    if row_a[0] == row_a[1] == row_a[2] == "x":
        x_Victory = True
    if row_b[0] == row_b[1] == row_b[2] == "x":
        x_Victory = True
    if row_c[0] == row_c[1] == row_c[2] == "x":
        x_Victory = True
    #vertical checks
    if row_a[0] == row_b[0] == row_c[0] == "x":
        x_Victory = True
    if row_a[1] == row_b[1] == row_c[1] == "x":
        x_Victory = True
    if row_a[2] == row_b[2] == row_c[2] == "x":
        x_Victory = True
    #diagonal checks
    if row_a[0] == row_b[1] == row_c[2] == "x":
        x_Victory = True
    if row_a[2] == row_b[1] == row_c[0] == "x":
        x_Victory = True
    else:
        x_Victory = False
   
    
    #checking for o wins
    #hortizontal checks
    if row_a[0] == row_a[1] == row_a[2] == "o":
        o_Victory = True
    if row_b[0] == row_b[1] == row_b[2] == "o":
        o_Victory = True
    if row_c[0] == row_c[1] == row_c[2] == "o":
        o_Victory = True
    #vertical checks
    if row_a[0] == row_b[0] == row_c[0] == "o":
        o_Victory = True
    if row_a[1] == row_b[1] == row_c[1] == "o":
        o_Victory = True
    if row_a[2] == row_b[2] == row_c[2] == "o":
        o_Victory = True
    #diagonal checks
    if row_a[0] == row_b[1] == row_c[2] == "o":
        o_Victory = True
    if row_a[2] == row_b[1] == row_c[0] == "o":
        o_Victory = True
    else:
        o_Victory = False
    

def x_turn():
    print()
    x_input = input("X PLAYS: ")
    x_input = x_input.upper()
    x_row = str(x_input[0])
    x_col = int(x_input[1])

    if x_row == "A":
        row_a[x_col] = str("x")
        DrawBoard()
    if x_row == "B":
        row_b[x_col] = str("x")
        DrawBoard()
    if x_row == "C":
        row_c[x_col] = str("x")
        DrawBoard()

def o_turn():
    print()
    o_input = input("O PLAYS: ")
    o_input = o_input.upper()
    o_row = str(o_input[0])
    o_col = int(o_input[1])

    if o_row == "A":
        row_a[o_col] = str("o")
        DrawBoard()
    if o_row == "B":
        row_b[o_col] = str("o")
        DrawBoard()
    if o_row == "C":
        row_c[o_col] = str("o")
        DrawBoard()

ResetGame()
DrawBoard()

x_Victory = False
o_Victory = False
 

for x in range(1,10):
    VictoryCheck()
    if x_Victory == True:
        print("X HAS WON")
    if o_Victory == True:
        print("X HAS WON")
    if x % 2 == 0:
        o_turn()
        VictoryCheck()  
    if not x % 2 == 0:
        x_turn()
        VictoryCheck()

我对 python 很陌生,所以不要判断草率的代码/术语。调用函数 VictoryCheck 并满足条件以使布尔变量 x_Victory 变为真

if x_Victory == True:
        print("X HAS WON")

不运行程序那种坐在那里有没有人知道问题可能是什么我愿意在需要时提供更多信息

标签: python

解决方案


在您的编程逻辑中,您需要使x_Victory全局:

def VictoryCheck():
    global x_Victory
    # ^^^

    #checking for x wins
    #hortizontal checks
    ...

否则,x_Victory仅存在于VictoryCheck()(被遮蔽)的范围内,并且您的全局变量保持不变。不过,全局变量往往会“污染”您的命名空间。您最好返回变量或使用类。


推荐阅读