非常新的编程,并希望帮助我的代码。 check_game()在填充时将row1check变为True。然后结束游戏应该开始,但它没有。我已经提供了打印声明来证明row1check确实变为true。
感谢您的帮助。
matrix =[]
row1 = [" ", " ", " "]
row2 = [" ", " ", " "]
row3 = [" ", " ", " "]
row1check = False
row2check = False
row3check = False
def draw_matrix ():
print row1
print row2
print row3
def check_game () :
if not " " in row1:
row1check = True
print row1check
if " " not in row2:
row2check = True
if " " not in row3:
row3check = True
def end_game() :
if row1check == True:
print "End of Game!"
else:
print "Keep going"
def x_turn ():
while end_game != 3 :
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()
if row == "3" and column == "1" and row3[0] == " ":
row3[0] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
if row == "3" and column == "2" and row3[1] == " ":
row3[1] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
if row == "3" and column == "3" and row3[2] == " ":
row3[2] = "x"
draw_matrix()
check_game()
end_game()
o_turn()
print "End of Game!"
def o_turn ():
while end_game != 3 :
move = raw_input("Player O Enter coordinates 'row,col': ")
row = move[0]
column = move[2]
if row == "1" and column == "1" and row1[0] == " ":
row1[0] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "1" and column == "2" and row1[1] == " ":
row1[1] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "1" and column == "3" and row1[2] == " ":
row1[2] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "2" and column == "1" and row2[0] == " ":
row2[0] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "2" and column == "2" and row2[1] == " ":
row2[1] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "2" and column == "3" and row2[2] == " ":
row2[2] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "3" and column == "1" and row3[0] == " ":
row3[0] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "3" and column == "2" and row3[1] == " ":
row3[1] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
if row == "3" and column == "3" and row3[2] == " ":
row3[2] = "O"
draw_matrix()
check_game()
end_game()
x_turn()
print "End of Game!"
x_turn()
答案 0 :(得分:0)
这是因为所有以下变量都需要使用关键字global声明,否则,在函数内部它将被视为局部变量
global matrix =[]
global row1 = [" ", " ", " "]
global row2 = [" ", " ", " "]
global row3 = [" ", " ", " "]
global row1check = False
global row2check = False
global row3check = False
而且,在函数中使用它们时,首先让python理解它与全局变量相同,在函数定义中再次将它定义为全局row1等。
def check_game () :
global row1check
global row2check
global row3check
global row1
global row2
global row3
if not " " in row1:
row1check = True
print row1check
if " " not in row2:
row2check = True
if " " not in row3:
row3check = True
因此,请记住关于全局变量的整个代码。