python - 为什么即使满足条件,这个布尔值也不为真?
问题描述
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")
不运行程序那种坐在那里有没有人知道问题可能是什么我愿意在需要时提供更多信息
解决方案
在您的编程逻辑中,您需要使x_Victory
全局:
def VictoryCheck():
global x_Victory
# ^^^
#checking for x wins
#hortizontal checks
...
否则,x_Victory
仅存在于VictoryCheck()
(被遮蔽)的范围内,并且您的全局变量保持不变。不过,全局变量往往会“污染”您的命名空间。您最好返回变量或使用类。
推荐阅读
- javascript - How to perform multiple axios POST requests in a For Loop with different body data for each iteration of loop?
- python - 如何从列表中匹配按字典顺序提到的键
- javascript - 分离两个 ID
- javascript - Javascript不会在html中将数据添加到表中
- django - Django:如何使用 ListView 将最后添加的日期添加到查询集中
- javascript - 使用 ajax 和 jquery 在烧瓶中构建类似的帖子失败
- typescript - 替代 Datasnapshot 的 foreach 循环,以便在满足条件时退出循环?
- python - 我在带有 espeak 库的 android 设备上的 pydroid 3 中遇到问题
- go - 发送数据完成后是否需要关闭通道?
- enterprise-architect - 连接形状脚本显示 GUID 不是参考元素的名称