Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 局部变量';模型预测&x27;分配前参考_Python - Fatal编程技术网

Python 局部变量';模型预测&x27;分配前参考

Python 局部变量';模型预测&x27;分配前参考,python,Python,[构建web应用程序以部署信件识别模型,但有错误,请提供帮助][1] def foo(): if some_condition: try: x = bar() # bar may throw a ValueError y = 2 except ValueError: return "bla bla message" return baz(x, y) 我得到了错误:赋值前引用的局部变量“y”如果第2行为False,则未设置y。这就是信

[构建web应用程序以部署信件识别模型,但有错误,请提供帮助][1]

def foo():
  if some_condition:
    try:
      x = bar()  # bar may throw a ValueError
      y = 2
    except ValueError:
      return "bla bla message"
  return baz(x, y)

我得到了错误:赋值前引用的局部变量“y”

如果第2行为
False
,则未设置
y
。这就是信息。您应该为
y
指定一些默认值:

def foo():
  y = some_default_value
  if True:
    try:
      x = bar()  # bar may throw a ValueError
      y = 2
    except ValueError:
      return "bla bla message"
  return baz(x, y)

您的变量定义在
if some_condition
条件语句中,但返回语句不是。。。因此,如果
some_condition
为false,则既不定义
x
也不定义
y

堆栈溢出问题的正确方法不是链接到代码,而是直接在问题中发布相关代码。这样我们就不必关注链接,您的问题将是不朽的,因为目标链接的内容可能随时发生变化。有关如何提问以获得正确答案的信息,请参阅。