Python 2.7 python井字游戏中的调用函数

Python 2.7 python井字游戏中的调用函数,python-2.7,Python 2.7,非常新的编程,并希望帮助我的代码。check_game()在填充行1check时将其变为True。然后结束游戏应该开始,但事实并非如此。我已经发表了书面声明来证明row1check确实变为true 谢谢你的帮助 matrix =[] row1 = [" ", " ", " "] row2 = [" ", " ", " "] row3 = [" ", " ", " "] row1check = False row2check = False row3ch

非常新的编程,并希望帮助我的代码。check_game()在填充行1check时将其变为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()

这是因为以下所有变量都需要用关键字global声明,否则,在函数中它将被视为局部变量

global matrix =[]

global row1 = [" ", " ", " "]
global row2 = [" ", " ", " "]
global row3 = [" ", " ", " "]
global row1check = False
global row2check = False
global row3check = False
而且,在函数中使用它们时,首先要让python理解它与全局变量相同,在函数定义中将它再次定义为全局行1,等等

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

因此,重写整个代码时要记住全局变量。

这是因为以下所有变量都需要用关键字global声明,否则,在函数中它将被视为局部变量

global matrix =[]

global row1 = [" ", " ", " "]
global row2 = [" ", " ", " "]
global row3 = [" ", " ", " "]
global row1check = False
global row2check = False
global row3check = False
而且,在函数中使用它们时,首先要让python理解它与全局变量相同,在函数定义中将它再次定义为全局行1,等等

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

因此,请重新编写整个代码,同时牢记全局变量。

第一次发布到该站点并第一次询问编码问题时,请修复所有缩进。我尽可能地修复压痕。你们只是从notepad++复制粘贴代码吗?当我这么做的时候,它没有给我论坛代码块。让我知道最好的方法。修复所有的缩进第一次张贴到这个网站,第一次问一个编码问题。我尽可能地修复压痕。你们只是从notepad++复制粘贴代码吗?当我这么做的时候,它没有给我论坛代码块。让我知道最好的办法。是的,先生,这就是答案global row1 row1=[“”,“”,“”,“”]@Thomas如果答案有用,请同时向上投票,因为StackOverflow用户可能会对答案的向下投票感到困惑/困扰。是的,先生,这就是答案。”global row1 row1=[“”,“”,“”]@Thomas如果答案有用,请同时向上投票,因为StackOverflow用户可能会对答案的向下投票感到困惑/困扰。