【问题标题】:How do I fix UnboundLocalError error?如何修复 UnboundLocalError 错误?
【发布时间】:2015-06-24 19:20:57
【问题描述】:

我编写了一个程序,该程序应该计算墙的周长,然后根据输入计算粉刷该墙所需的油漆成本。

代码:

def main():
length = float(input('Enter length: ')) #Get length.
width = float(input('Enter width: ' )) #Get width.
height = float(input('Enter height: ')) #Get height.
Paint_cost()
def Paint_cost (length, width, height): #Find total paint cost.
    perimeter = length + width *4 #Find perimiter.
sq_ft = perimeter * height #Find total sq. ft. amount.
Paint = sq_ft / 300 #Calculate paint gallons to nearest int.
round(Paint)
Total= Paint*40 #Calculate total cost.
return total #Display total.

main()

但是 Python 一直说“UnboundLocalError:在赋值之前引用了局部变量 'Paint_cost'”。我在这里做错了什么?

【问题讨论】:

  • 请修正缩进。除了它总是有利于任何语言的任何代码的可读性,并且对于能够调试 Python 代码总是必不可少的这一事实之外,在这种情况下,您的代码的实际问题很可能是缩进,所以我们尤其是 需要看看你是如何缩进的。
  • @Tutleman:我有 80% 的把握确定您的编辑实际上消除了 OP 的实际问题,因此这是一个无用的问题。不要试图猜测他的缩进可能是什么,而是让他告诉我们它实际上是什么
  • @abarnert 你说得对。就在你做回归的时候,我正要做同样的事情。鉴于他遇到的错误,我同意你的看法,这个错误很可能是缩进之一。
  • 我很想知道你正在运行的python的平台和版本,因为运行它时我得到了不同的结果。
  • @NKamrath:好吧,当按照发布的方式运行时,您将在任何版本的 Python 上获得 IndentationError。在他向我们展示他的实际代码之前,它并不是很有用。

标签: python


【解决方案1】:

你有几个问题:

  • 首先,您在main() 内定义函数Paint_cost()。你可以在main()之外定义它,只要在你调用main()函数之前定义,它就可以正常工作。
  • 其次,return 从函数返回一个值,而不是打印它。
  • 第三,您的缩进已关闭。不管其他两个错误如何,如果您尝试运行它,Python 将引发 IndentationError
  • 第四,total 未定义(你写成Total。)
  • 最后,您在没有任何参数的情况下调用Paint_cost()。您需要使用Paint_cost(length, width, height) 调用它。

这段代码在 Python 3 中完美运行:

def Paint_cost (length, width, height): #Find total paint cost.
    perimeter = length + width * 4 #Find perimiter.
    sq_ft = perimeter * height #Find total sq. ft. amount.
    Paint = sq_ft / 300 #Calculate paint gallons to nearest int.
    int(Paint)
    total = Paint*40 #Calculate total cost.
    return total #Display total.
def main():

    length = float(input('Enter length: ')) #Get length.
    width = float(input('Enter width: ' )) #Get width.
    height = float(input('Enter height: ')) #Get height.
    print(Paint_cost(length, width, height)) # Print the cost of the paint.

main()

这个适用于 Python 2:

def Paint_cost (length, width, height): #Find total paint cost.
    perimeter = length + width * 4 #Find perimiter.
    sq_ft = perimeter * height #Find total sq. ft. amount.
    Paint = sq_ft / 300 #Calculate paint gallons to nearest int.
    int(Paint)
    total = Paint*40 #Calculate total cost.
    return total #Display total.
def main():

    length = float(input('Enter length: ')) #Get length.
    width = float(input('Enter width: ' )) #Get width.
    height = float(input('Enter height: ')) #Get height.
    print Paint_cost(length, width, height)  # Print the cost of the paint.

main()

在这段代码中,print 是 Python 2 和 3 之间的唯一变化。该函数在任一版本中都无需打印即可工作。
如果有问题请告诉我。

【讨论】:

  • 这修复了它。只是出于好奇,您为什么要交换两个定义位置?我认为他们必须是这样的?
  • @KernelPanic 哪两个定义的地方?
  • 实际上这是很多答案的假设,但是 python 会在函数乱序的情况下运行它,因为它会在调用它的主运行之前绑定 Paint_cost。我建议@ASCIIThenANSI 更改他的这部分答案。这在技术上是合法的,并且会运行。将其放入文件中并试一试。
  • @NKamrath 你的意思是把Paint_cost函数移到main下面?还是别的什么?
  • 我的意思是函数的顺序不会影响它。尝试使用上面定义的 main 油漆成本运行它,它会运行。因为 main 是在绘制成本定义后调用的。
【解决方案2】:

但是 Python 一直说“UnboundLocalError:在赋值之前引用了局部变量 'Paint_cost'”。我在这里做错了什么?

正是它所说的:

Paint_cost()
def Paint_cost (length, width, height): #Find total paint cost.
    perimeter = length + width *4 #Find perimiter.
    # ...

您在定义之前调用了Paint_cost(),因此它还没有可调用的内容,因此会引发异常。


要修复它,请不要这样做。将调用移至定义后的Paint_cost。或者将Paint_cost 的定义移到全局级别。 (如果它没有从main 访问任何局部变量,则不需要在main 中定义。)


该消息可能有点令人困惑,因为它指的是“分配”,而您正在做的是def Paint_cost(…):,而不是Paint_cost = …。但是def 是一种赋值,因为它绑定了一个新名称。

【讨论】:

    【解决方案3】:

    您在定义之前调用了函数Paint_cost。将 Paint_cost 函数移到 main 之外,您的程序应该可以正常运行。

    def Paint_cost (length, width, height): #Find total paint cost.
        return length + width *4 #Find perimiter.
        sq_ft = perimeter * height #Find total sq. ft. amount.
        Paint = sq_ft / 300 #Calculate paint gallons to nearest int.
        round(Paint)
        return Paint*40 #Calculate total cost.
    
    def main():
        length = float(input('Enter length: ')) #Get length.
        width = float(input('Enter width: ' )) #Get width.
        height = float(input('Enter height: ')) #Get height.
        return Paint_cost(length, width, height)
    
    main()
    

    其他说明:您在main() 的最后一行返回total,但这也没有在任何地方定义。 Python 区分大小写,所以totalTotal 不是一回事。

    【讨论】:

    • 好吧,解决了一个问题。现在 Python 说“NameError: name 'perimeter' is not defined”。即使是这样。
    • 尝试运行我上面得到的确切代码。我将其更改为将perimeter 的值设置为Paint_cost 的返回值。如果可行的话,LMK!
    • @KernelPanic:如果您有多个错误,不要指望有人在 StackOverflow 上为您完成所有调试。如果这个问题解决了,接受一个答案,尝试自己解决下一个问题,如果/当你遇到困难时,创建一个新问题(链接到这个问题,如果它是相关的)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 2016-11-17
    相关资源
    最近更新 更多