Python 本地变量 'Current_Balance' 在赋值前被引用

2024-10-05 10:11:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么会这样。我已经试着修好一段时间了

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

错误:

^{pr2}$

Tags: textinidbyifdriverelementcurrent
1条回答
网友
1楼 · 发布于 2024-10-05 10:11:05

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

当您直接通过"Rolling in 28." in Label.text路径时,尚未创建此变量。在

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

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

注意,按照惯例,变量名通常以非大写字母开头(类名最好使用大写字母)。在

相关问题 更多 >

    热门问题