Python(3.3):UnboundLocalError:赋值前引用的局部变量

时间:2016-02-17 23:09:20

标签: python python-3.x

Paul Rooney解决: 在xcode的顶部放置了

的全局

我已经搜索了其他网站和整个堆栈器溢出并且无法找到解决方案,这是一个错误的独特案例,因为我无法在定义中声明变量。 每当我开始我的游戏时,让我们说例如我输入1,4作为网格参考,它将回复

  

“UnboundLocalError:'在赋值前引用'的局部变量'

我该如何解决这个问题?

代码:

import time
while 1==1:
    cont=1
    while cont==1:

        of="-"
        tf="-"
        rf="-"
        ov="-"
        tv="-"
        rv="-"
        os="-"
        ts="-"
        rs="-"
        go=1

        def grid(g):
            print (" ")
            print ("     1  2  3")
            print ("4   {} | {} | {}". format(of,tf,rf))
            print ("     --------")
            print ("5   {} | {} | {}". format(ov,tv,rv))
            print ("     --------")
            print ("6   {} | {} | {}". format(os,ts,rs))
            print (" ")


        def xturn(x):
            go=1
            while go==1:
                goo=input("X-Insert Board Reference: ")
                go=0
                if goo in "1,4" and of not in "o":
                    of="x"
                elif goo in "2,4" and tf not in "o":
                    tf="x"
                elif goo in "3,4" and rf not in "o":
                    rf="x"
                elif goo in "1,5" and ov not in "o":
                    ov="x"
                elif goo in "2,5" and tv not in "o":
                    tv="x"
                elif goo in "3,5" and rv not in "o":
                    rv="x"
                elif goo in "1,6" and os not in "o":
                    os="x"
                elif goo in "2,6" and ts not in "o":
                    ts="x"
                elif goo in "3,6" and rs not in "o":
                    rs="x"
                else:
                    print ("NOT RECOGNISED/POSITION TAKEN")
                    go=1

        def oturn(o):
            go=1
            while go==1:
                goo=input("O-Insert Board Reference: ")
                go=0
                if goo in "1,4" and of not in "x":
                    of="o"
                elif goo in "2,4" and tf not in "x":
                    tf="o"
                elif goo in "3,4" and rf not in "x":
                    rf="o"
                elif goo in "1,5" and ov not in "x":
                    ov="o"
                elif goo in "2,5" and tv not in "x":
                    tv="o"
                elif goo in "3,5" and rv not in "x":
                    rv="o"
                elif goo in "1,6" and os not in "x":
                    os="o"
                elif goo in "2,6" and ts not in "x":
                    ts="o"
                elif goo in "3,6" and rs not in "x":
                    rs="o"
                else:
                    print ("NOT RECOGNISED/POSITION TAKEN")
                    go=1

        def xcheck(xc):
            if of in "x" and tf in "x" and rf in "x":
              print ("X WINS!")
              cont=0
            elif ov in "x" and tv in "x" and rv in "x":
              print ("X WINS!")
              cont=0
            elif os in "x" and ts in "x" and rs in "x":
              print ("X WINS!")
              cont=0
            elif of in "x" and ov in "x" and os in "x":
              print ("X WINS!")
              cont=0
            elif tf in "x" and tv in "x" and ts in "x":
              print ("X WINS!")
              cont=0
            elif rf in "x" and rv in "x" and rs in "x":
              print ("X WINS!")
              cont=0
            elif of in "x" and tv in "x" and rs in "x":
              print ("X WINS!")
              cont=0
            elif rf in "x" and tv in "x" and os in "x":
              print ("X WINS!")
              cont=0
            else:
              grid(1)
              oturn(1)

        def ocheck(oc):
            if of in "o" and tf in "o" and rf in "o":
              print ("O WINS!")
              cont=0
            elif ov in "o" and tv in "o" and rv in "o":
              print ("O WINS!")
              cont=0
            elif os in "o" and ts in "o" and rs in "o":
              print ("O WINS!")
              cont=0
            elif of in "o" and ov in "o" and os in "o":
              print ("O WINS!")
              cont=0
            elif tf in "o" and tv in "o" and ts in "o":
              print ("O WINS!")
              cont=0
            elif rf in "o" and rv in "o" and rs in "o":
              print ("O WINS!")
              cont=0
            elif of in "o" and tv in "o" and rs in "o":
              print ("O WINS!")
              cont=0
            elif rf in "o" and tv in "o" and os in "o":
              print ("O WINS!")
              cont=0
            else:
              grid(1)
              xturn(1)


        ocheck(1)

        xcheck(1)

        ocheck(1)

        xcheck(1)

        ocheck(1)

        xcheck(1)

        ocheck(1)

        xcheck(1)

        ocheck(1)

        xcheck(1)

        print ("THERE WAS NO WINNER")

回溯:

Traceback (most recent call last):
  File "b4assign.py", line 141, in <module>
    ocheck(1)
  File "b4assign.py", line 138, in ocheck
    xturn(1)
  File "b4assign.py", line 33, in xturn
    if goo in "1,4" and of not in "o":
UnboundLocalError: local variable 'of' referenced before assignment

3 个答案:

答案 0 :(得分:2)

即使仅在外部范围内定义变量,访问其中一个函数内的变量也基本可行。但是在xturn函数中,您有一行分配给of。在发生错误之前是否已执行此行并不重要;它仅存在于函数内的任何地方都会导致Python解释器将其视为局部变量。因此,当在if子句中访问它时,Python会尝试访问本地变量of,现在确实在此之前尚未分配这样的局部变量。

答案 1 :(得分:1)

的变量(以及所有其他双字母变量)在 xturn 功能中不可用。

我强烈建议您使用增量编程:编写几行代码,使其工作,然后扩展您的程序。这使您可以非常轻松地将错误归零。每当我攻击一种新的编程风格时,我发现这非常有用。您似乎对此处使用的某些技术不熟悉。

例如,您使用 而不是 == 进行比较。作为一般原则,这不会很好。

在主程序之前声明你的函数。你写这篇文章的方式,每次循环都重新定义你的函数。将函数移动到顶部也可以解决许多变量范围问题。

学习使用布尔值和变量。你的循环应该是这样的:

while True:
    cont = True
    while cont:

通过将变量作为参数传递给函数,可以使变量可用。我可以看到你对此不熟悉,因为你已经为这个函数提供了一个你永远不会使用的参数 x

总的来说,你不应该有9个变量:你应该有一个列表,然后将整个列表作为游戏板的当前状态传递。如果您将方块编号为0-8,则可以在这方面轻松使用该板。

要解决当前问题,您可以将此行添加到您的每个例程中:

global of,tf,rf,ov,tv,rv,os,ts,rs

这将使变量可用。我看到@Thomas已经指出了这一点。

不过,我鼓励你更干净地设计这个。使用全局变量通常是糟糕的设计。另外,请注意您需要为此程序复制多少代码?它应该更容易。

答案 2 :(得分:0)

在函数xturn中,使用在该范围内未声明/已知的变量of,从而导致错误。

如果查看堆栈跟踪:

Traceback (most recent call last):
  File "D:/Projekte/Python/snakes35/blabla.py", line 141, in <module>
    ocheck(1)
  File "D:/Projekte/Python/snakes35/blabla.py", line 138, in ocheck
    xturn(1)
  File "D:/Projekte/Python/snakes35/blabla.py", line 33, in xturn
    if goo in "1,4" and of not in "o":
UnboundLocalError: local variable 'of' referenced before assignment

你可以通过查看文件中出现错误的行来解决这个问题。