Python UnBoundLocalError:分配前引用了局部变量 [计数器]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17521410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:24:35  来源:igfitidea点击:

UnBoundLocalError: Local Variable Referenced Before Assignment [Counter]

python

提问by Justin

I'm new to Python and I've never learned any other programming language. I seem to be getting this error and I've read other posts but they say to put global before [dollars = 0] which produces a syntax error because it doesn't allow the [= 0]. I'm using the [dollars] as a counter so I can keep track of what I add to it and display it back when needed. Could someone help me? Thanks.

我是 Python 新手,从未学过任何其他编程语言。我似乎遇到了这个错误,我已经阅读了其他帖子,但他们说将 global 放在 [dollars = 0] 之前,这会产生语法错误,因为它不允许 [= 0]。我使用 [dollars] 作为计数器,这样我就可以跟踪我添加的内容并在需要时显示回来。有人可以帮助我吗?谢谢。

<>Code<>

<>代码<>

    dollars = 0

    def sol():
        print('Search or Leave?')
        sol = input()
        if sol == 'Search':
            search()
        if sol == 'Leave':
            leave()

    def search():
        print('You gain 5 bucks')
        dollars = dollars + 5
        shop()

    def leave():
        shop()

    def shop():
        shop = input()
        if shop == 'Shortsword':
            if money < 4:
                print('I\'m sorry, but you don\'t have enough dollars to buy that item.')
                shop1()
            if money > 4:
                print('Item purchased!')
                print('You now have ' + dollars + ' dollars.')

    sol()


<>Traceback<>

<>追溯<>

Traceback (most recent call last):
  File "C:/Users/justin/Python/Programs I Made/Current/Testing.py", line 29, in <module>
    sol()
  File "C:/Users/justin/Python/Programs I Made/Current/Testing.py", line 7, in sol
    search()
  File "C:/Users/justin/Python/Programs I Made/Current/Testing.py", line 13, in search
    dollars = dollars + 5
UnboundLocalError: local variable 'dollars' referenced before assignment

采纳答案by Sukrit Kalra

You need to add global dollars, like follows

您需要添加global dollars,如下所示

def search():
    global dollars
    print('You gain 5 bucks')
    dollars = dollars + 5
    shop()

Everytime you want to change a globalvariable inside a function, you need to add this statement, you can just access the dollarvariable without the globalstatement though,

每次要更改global函数内部的变量时,都需要添加此语句,但dollar无需该global语句即可访问该变量,

def shop():
    global dollars
    shop = input("Enter something: ")
    if shop == 'Shortsword':
        if dollars < 4:          # Were you looking for dollars?
            print('I\'m sorry, but you don\'t have enough dollars to buy that item.')
            shop1()
        if dollars > 4:
            print('Item purchased!')
            dollars -= someNumber # Change Number here
            print('You now have ' + dollars + ' dollars.')

You also need to reduce the dollars, when you shop for something!

当您购物时,您还需要减少美元!

P.S - I hope you're using Python 3, you'll need to use raw_inputinstead.

PS - 我希望你使用的是 Python 3,你需要使用它raw_input

回答by Daniel Roseman

You need to put global dollars, on a line on its own, inside any function where you change the value of dollars. In the code you've shown that is only in search(), although I assume you'll also want to do it inside shop()to subtract the value of the item you buy...

您需要将 单独global dollars放在一行中,放在您更改美元价值的任何函数中。在您显示的代码中,它仅在 中search(),尽管我假设您还想在内部进行shop()以减去您购买的物品的价值...