UnBoundLocalError:赋值前引用的局部变量[计数器][复制]

时间:2021-03-13 22:48:44

This question already has an answer here:

这个问题已经有了答案:

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新手,从来没有学过其他编程语言。我似乎得到了这个错误,我读过其他的帖子,但是他们说,在[美元= 0]之前把全局写出来,这会产生一个语法错误,因为它不允许[= 0]。我使用[美元]作为计数器,这样我就可以跟踪我添加的东西,并在需要时显示它。有人能帮我吗?谢谢。

<>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

2 个解决方案

#1


16  

You need to add global dollars, like follows

您需要添加全球美元,如下所示。

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

Everytime you want to change a global variable inside a function, you need to add this statement, you can just access the dollar variable without the global statement though,

每次你想要在函数内改变一个全局变量时,你需要添加这个语句,你可以在没有全局声明的情况下访问美元变量,

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_input instead.

P。我希望您使用的是python3,您需要使用raw_input。

#2


1  

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...

你需要把全球的美元,放在自己的线上,在任何你改变美元价值的地方。在您所展示的代码中,只有在search()中,尽管我假设您也想在shop()中做它来减去您购买的项目的值……

#1


16  

You need to add global dollars, like follows

您需要添加全球美元,如下所示。

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

Everytime you want to change a global variable inside a function, you need to add this statement, you can just access the dollar variable without the global statement though,

每次你想要在函数内改变一个全局变量时,你需要添加这个语句,你可以在没有全局声明的情况下访问美元变量,

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_input instead.

P。我希望您使用的是python3,您需要使用raw_input。

#2


1  

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...

你需要把全球的美元,放在自己的线上,在任何你改变美元价值的地方。在您所展示的代码中,只有在search()中,尽管我假设您也想在shop()中做它来减去您购买的项目的值……