Python(New)为什么此函数不打印分配给它的新变量?

时间:2019-04-24 19:33:35

标签: python-3.x

我目前正在学习python作为我的第一门编程语言,并认为我已经掌握了知识,并想挑战自己来创建井字游戏。我已经将代码工作到了开始游戏的地步,并要求先走一步,但是当我尝试打印新的棋盘时,他的空间就不会打印出来。

L1 = " "

def board():
  print("     |     |        ")
  print(" ",L1," | ",M1," | ",R1," ")
  print("     |     |   ")
  print("-----------------")
  print("     |     |   ")
  print(" ",L2," | ",M2," | ",R2," ")
  print("     |     |   ")
  print("-----------------")
  print("     |     |   ")
  print(" ",L3," | ",M3," | ",R3," ")
  print("     |     |   ")

Xmove = input("Where does X want to go? ")

def xmove(Xmove):

   if Xmove == ("L1"):
     L1 = "X"
     board()

xmove(Xmove)

这应该在新板上打印,左上角的空格现在是“ X”,但不是。它只是打印一个空白板。

3 个答案:

答案 0 :(得分:1)

您可能需要移动一些内容并使用字典,因此更容易跟踪。这也避免了对全局变量的需求。这也可以通过简单的代码方便地处理您的所有潜在动作。

def board():
    print("     |     |        ")
    print("   "+pm['L1']+" |   "+pm['M1']+"  | "+pm['R1']+" ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print("   "+pm['L2']+" |   "+pm['M2']+"  | "+pm['R2']+" ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print("   "+pm['L3']+" |   "+pm['M3']+"  | "+pm['R3']+" ")
    print("     |     |   ")


pm = {'L1': '', 'M1': '', 'R1': '',
      'L2': '', 'M2': '', 'R2': '',
      'L3': '', 'M3': '', 'R3': '', }


def xmove():
    Xmove = input("Where does X want to go? ")
    pm[Xmove] = 'X'
    board()

xmove()

答案 1 :(得分:0)

现在,您已经将L1设置为全局变量。您可以在函数内部访问全局变量,但是要更改它们,您需要使用global关键字,如下所示:

def xmove(Xmove):
    global L1
    if Xmove == ("L1"):
        L1 = "X"
        board()

答案 2 :(得分:0)

我希望您能明白为什么使用这样的全局变量可能会很乏味-您每次要使用它们时都必须将它们声明为全局变量。对于您的情况,您必须在每个需要访问它们的函数中声明9个全局变量。相反,您可以使用类方法将它们分组在一起。然后,您所要做的就是让全班同学通过。它可能看起来像这样:

class BoardClass:
    def __init__(self):
        # This is the constructor function that is run when you create a
        # a new instance of BoardClass. All we'll do is set the default 
        # values. 'self' is exactly what it sounds like. *itself*
        self.L1 = " "
        self.M1 = " "
        self.R1 = " "
        self.L2 = " "
        self.M2 = " "
        self.R2 = " "
        self.L3 = " "
        self.M3 = " "
        self.R3 = " "

def board(b):
    # b is the BoardClass object. You can refer to its members with b.varname
    print("     |     |        ")
    print(" ",b.L1," | ",b.M1," | ",b.R1," ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print(" ",b.L2," | ",b.M2," | ",b.R2," ")
    print("     |     |   ")
    print("-----------------")
    print("     |     |   ")
    print(" ",b.L3," | ",b.M3," | ",b.R3," ")
    print("     |     |   ")

def xmove(b, Xmove):
    # Again, b is the BoardClass object.
    if Xmove == ("L1"):
        b.L1 = "X" # You can set the parameters of b like this
        board(b)

# Create your board
b = BoardClass() 

# Ask user for move
Xmove = input("Where does X want to go? ")

# Make the move and print
xmove(b, Xmove)

尽管这只是一招,但您必须想出自己的逻辑来使游戏循环播放并切换转弯。