包含先前创建的变量的列表出现问题。错误局部变量“胜利”赋值前引用
我有曲棍球比赛的胜利、平局、失利变量。显然,根据游戏的最终结果,这些变量中只有一个为 True。因此,我在 try and except: pass 中创建了它们。
现在我只想打印这些变量之一(True 变量,例如胜利,因为胜利根据曲棍球比赛结果的抓取而变为 True),但我收到错误 UnboundLocalError: local variable '当我搜索打印 True 变量时,在赋值之前引用了 Victory',因为 Victory 在赋值之前已经被命名了。鉴于问题的简单性,我只报告对解决方案有用的部分:
也许我不应该使用列表
#scraping and assignment of Point_Team_A and Point_Team_B
...
try:
Victory = Point_Team_A > Point_Team_B
Draw = Point_Team_A == Point_Team_B
Lose = Point_Team_A < Point_Team_B
except:
pass
result=[Victory, Draw, Lose] #great problem is here
print (*filter(None, result), sep='\n') #and here
例如,如果结果是胜利,我想获得输出:Victory
注意:我使用 try / except 因为在 Victory、Draw、Lose 之间只有一 (1) 个变量为 True。这两个 False 变量将被识别为错误,因为我之前在 if / else 条件内创建了 Point_Team_A 和 Point_Team_B。随后我使用 Point_Team_A 和 B 创建上面的 3 个变量。例如,如果曲棍球比赛的结果是7-3,则意味着A队获胜,因为Victory=Point_Team_A>A队。 Point_Team_B。因此,Python 会将 Draw 和 Lose 识别为错误,为此请使用 try / except。
I have Victory, Draw, Lose variables for a hockey game. Obviously, based on the final result of a game, only one of these variables will be True. For this reason I created them inside a try and except: pass.
Now I would like to print only one of these variables (the True one, for example Victory, because Victory becomes True based on a scraping of the results of the hockey match), but I get the error UnboundLocalError: local variable 'Victory' referenced before assignment
when I search to print the True variable, because Victory was already named before the assignment. Given the simplicity of the problem, I report only the part useful for the resolution:
Maybe I shouldn't use a list
#scraping and assignment of Point_Team_A and Point_Team_B
...
try:
Victory = Point_Team_A > Point_Team_B
Draw = Point_Team_A == Point_Team_B
Lose = Point_Team_A < Point_Team_B
except:
pass
result=[Victory, Draw, Lose] #great problem is here
print (*filter(None, result), sep='\n') #and here
For example if the result is a Victory, I want to get the output: Victory
NOTE: I use try / except because only one (1) variable will be True between Victory, Draw, Lose. The two False variables will be recognized as an error, because I previously created Point_Team_A and Point_Team_B inside an if / else condition. Subsequently I use Point_Team_A and B to create the 3 variables above. For example, if the result of a hockey match is 7-3, it means that team A has won, because Victory = Point_Team_A> Point_Team_B. Consequently Draw and Lose will be recognized by Python as an error, for this use try / except.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要按原样执行
你的代码,当它工作时,实际上只会打印
True
如果你想要另一种方式来重现你所看到的这个错误,试试这个:
发生的事情是 python 解释器知道名称
x
存在于范围内,但它永远不会绑定到任何东西(因为异常发生在赋值之前)。所以你会得到UnboundLocalError
just do
your code as is, when it works, would literally only ever print
True
if you want another way to reproduce this error you're seeing, try this:
what's happening is that the python interpreter knows that the name
x
exists in scope but also that it's never bound to anything (because the exception occurs before the assignment). so you getUnboundLocalError
Python变量分为两个类别之一: Global 和 local 。如果变量在文件范围上定义,则变量为全局。相反,局部变量是在 python块中定义的变量。在Python块中,我的意思是,与其他语言不同(例如C ++),介绍了介绍新的代码块(尤其是函数定义)的任何Python构造
,可以从外部范围访问本地Python变量。这通常会提高 unboundlocalerror 例外。考虑以下代码:
check_age()
正常工作,直到使用值 18 调用它。因为check_age()
中的条件语句都不当函数与值18调用时,具有值(未结合)。因此,在您的特定情况下,访问
info
以后通过返回语句提出 unboundlocalerror,
定义了您随身携带的新块(或范围)为几个变量分配一些作业。稍后,您将在封闭范围中访问这些变量。
unboundlocalerror
的唯一明显的原因是,在您的try
block(尤其是在第一行)中提出了例外,该异常被Pass沉默
在您的中,
块除外,导致victor
不分配一个值。因此,尝试通过list = [胜利,绘制,丢失] 提出上述例外。这可能仅意味着将值传递到
try
block中第一行的&gt;
运算符时存在问题。至少一个值未定义运算符。底线是检查您的刮擦数据,以确保您传递正确的值。
Python variables fall into one of two categories: global and local. A variable is global if it is defined at file scope. Contrary, a local variable is one that is defined within a python block. By a python block, I mean any of the python constructs that introduces a new code block delimited by indentation(notably a function definition)
Unlike other languages(C++ for instance), local python variables can be accessed from an outside scope. This is what typically raises an UnboundLocalError exception. Consider the following code:
check_age()
works just fine until it is invoked with the value 18. Because none of the conditional statements withincheck_age()
defines the value ofinfo
whenage
is 18,info
wont have a value(unbound) when the function is invoked with the value 18.Consequently, accessing
info
later through the return statement raises an UnboundLocalErrorIn your specific situation,
try
defines a new block(or scope) where you carry out some assignments to a couple of variables. You later access those variables in an enclosing scope.The only immediately obvious cause of an
UnboundLocalError
is that an exception is raised within yourtry
block(particularly on the first line) which is silenced bypass
in yourexcept
block, causingVictory
not to be assigned a value. Hence trying to accessVictory
throughlist = [Victory, Draw, Lose]
raises the said exception. This could only mean that there is a problem with the values being passed to the>
operator on the first line within yourtry
block. Probably the operator is not defined for at least one of the values.The bottomline is to check your scraped data to ensure that you are passing the right values.
当尝试块中有例外时,在try块中创建的变量将在外部尝试块中不可用,因此创建一个变量并在外部尝试块中或内部初始化它,除非
When there is an exception in the try block the variables created in the try block will not be available in an outside try block, so create a variable and initialize it either in the outside try block or inside except