简体   繁体   English

Python(3.3):UnboundLocalError:分配前已引用局部变量

[英]Python (3.3): UnboundLocalError: local variable referenced before assignment

Whenever I start my game and let's say for example I enter 1,4 as the grid reference it will reply with 每当我开始游戏时,例如说我输入1,4作为网格参考,它将以

"UnboundLocalError: local variable 'of' referenced before assignment" “ UnboundLocalError:分配前引用了局部变量'of'”

How can I fix this? 我怎样才能解决这个问题?

Code (edited to for brevity): 代码(为简便起见,编辑为:)

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

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

Accessing a variable inside one of your functions will basically work even if the variable is defined only in an outside scope. 即使仅在外部作用域中定义变量,在函数之一内部访问变量也将基本起作用。 But in your xturn function, you have a line that assigns to of . 但在你xturn功能,你有一个行分配给该of It doesn't matter whether this line has been executed before the error occurs; 错误发生之前是否已执行此行并不重要; its mere existence anywhere inside the function causes the Python interpreter to treat it as a local variable. 它仅存在于函数内部的任何地方,都会导致Python解释器将其视为局部变量。 Therefore, when accessing it in the if clause, Python tries to access a local variable of and now it does matter that such a local variable hasn't been assigned up until that point. 因此,当在if子句中访问它时,Python尝试访问的局部变量of而现在直到那时才分配这样的局部变量确实很重要。

Variable of (and all the other 2-letter variables) are not available in function xturn . 变量(和所有其他2个字母的变量)是不是在功能xturn可用。

I strongly recommend that you use incremental programming: write a few lines of code, get those working, and then enlarge your program. 我强烈建议您使用增量编程:编写几行代码,使这些代码正常工作,然后扩大程序。 This allows you to zero in on errors quite easily. 这使您可以轻松地将错误归零。 Any time I attack a new style of programming, I find this quite useful. 每当我攻击一种新的编程风格时,我都会发现它非常有用。 It appears that you're new to some of the techniques you're using here. 您似乎不熟悉此处使用的某些技术。

For instance, you're using in instead of == for comparisons. 例如,您使用in而不是==进行比较。 This is not going to work well as a general principle. 作为一般原则,这行不通。

Declare your functions before the main program. 在主程序之前声明您的功能。 The way you wrote this, you're redefining your functions every time you go through the loop. 编写此代码的方式是,每次循环时都要重新定义函数。 Moving functions to the top will cure you of many variable scoping problems, too. 将函数移到顶部也可以解决许多可变范围问题。

Learn to use Boolean values and variables. 学习使用布尔值和变量。 Your loops should look like this: 您的循环应如下所示:

while True:
    cont = True
    while cont:

You make variables available by passing them as arguments to the function. 您可以通过将变量作为参数传递给函数来使其可用。 I can see that you're new to this, because you've given this function a parameter x that you never use. 我可以看到您对此并不陌生,因为已经为该函数提供了一个从未使用过的参数x

Overall, you should not have 9 variables: you should have a list, and then just pass the entire list as the current state of the game board. 总的来说,您不应有9个变量:您应该有一个列表,然后将整个列表作为游戏板的当前状态传递。 If you number the squares 0-8, you can easily work with the board in that respect. 如果将0-8平方数编号,则可以轻松地在此方面使用该板。


To solve the immediate problem, you can add this line to each of your routines: 要解决眼前的问题,可以将以下行添加到每个例程中:

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

This will make the variables available. 这将使变量可用。 I see that @Thomas has pointed this out. 我看到@Thomas指出了这一点。

Still, I encourage you to work on designing this more cleanly. 尽管如此,我还是鼓励您更加简洁地进行设计。 Using global variables is generally bad design. 使用全局变量通常是不好的设计。 Also, notice how much code you have to duplicate for this program? 另外,请注意您必须为该程序复制多少代码? It should be a lot easier. 应该容易得多。

In your function xturn you use the variable of which is not declared / known in that scope, thus the error. 在您的函数xturn使用变量of未申报/在该范围内已知的,因此错误。

If you look at the stack trace: 如果查看堆栈跟踪:

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

you can figure that out via looking at the lines in your file, where the error ocures. 您可以通过查看文件中发生错误的行来找出问题所在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) python - UnboundLocalError:分配前引用的局部变量 - python - UnboundLocalError: local variable referenced before assignment Python-UnboundLocalError:分配前引用的局部变量 - Python - UnboundLocalError: local variable referenced before assignment` Python UnboundLocalError:分配前引用了局部变量 - Python UnboundLocalError: local variable referenced before assignment Python | 如果变量:| UnboundLocalError:赋值前引用的局部变量&#39;variable&#39; - Python | if variable: | UnboundLocalError: local variable 'variable' referenced before assignment UnboundLocalError:在python闭包中赋值之前引用的局部变量 - UnboundLocalError: local variable referenced before assignment in python closure Python 分裂问题 UnboundLocalError:分配前引用了局部变量“e” - Python Splinter issue UnboundLocalError: local variable 'e' referenced before assignment UnboundLocalError:分配前引用的局部变量“转” - python - UnboundLocalError: local variable 'turn' referenced before assignment - python 如何修复 UnboundLocalError:在 Python 中分配之前引用的局部变量“df” - How to fix UnboundLocalError: local variable 'df' referenced before assignment in Python UnboundLocalError:在 Python3 中赋值之前引用了局部变量“sumOfOdd” - UnboundLocalError: local variable 'sumOfOdd' referenced before assignment in Python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM