在赋值之前引用了Python局部变量’Current_Balance’

Python local variable 'Current_Balance' referenced before assignment

我不知道为什么会这样。想修一段时间了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def Bettings():
    while True:
        if"Rolling in 35." in Label.text:
            Updated_Balance = driver.find_element_by_xpath("""//*[@id="balance"]""")
        if"Rolling in 23." in Label.text:
            Current_Balance = driver.find_element_by_xpath("""//*[@id="balance"]""")

        if"Rolling in 28." in Label.text:

            if Current_Balance < Updated_Balance:

                GrayBetButton.click()
            if Current_Balance > Updated_Balance:
                RedBetButton.click()

Bettings()

错误:

1
UnboundLocalError: local variable 'Current_Balance' referenced before assignm


只有在通过"Rolling in 23." in Label.text路径时,才定义变量Current_Balance

当您直接通过"Rolling in 28." in Label.text路径时,这个变量还没有被创建。

您可能希望在顶部创建此变量,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def Bettings():
    current_balance = 0
    while True:
        if"Rolling in 35." in Label.text:
            updated_balance = driver.find_element_by_xpath("""//*[@id="balance"]""")
        if"Rolling in 23." in Label.text:
            current_balance = driver.find_element_by_xpath("""//*[@id="balance"]""")
        if"Rolling in 28." in Label.text:
            if current_balance < updated_balance:
                grayBetButton.click()
            if current_Balance > updated_balance:
                redBetButton.click()

Bettings()

注意,按照惯例,变量名往往以非大写字母开头(类名最好是大写)。