2016-03-02 125 views
-2

Idon不知道爲什麼會發生這種情況。一直試圖修復了一段時間,現在在分配之前引用的Python局部變量'Current_Balance'

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

錯誤:

UnboundLocalError: local variable 'Current_Balance' referenced before assignm 
+0

可能重複[Local(?)變量在賦值之前引用](http://stackoverflow.com/questions/11904981/local-variable-referenced-before-assignment) – schwobaseggl

回答

2

你定義的變量Current_Balance只有當你經過"Rolling in 23." in Label.text「路徑」。

當您直接通過"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() 

注意,按照慣例,變量名往往先從非大寫字母(大小寫是優選的類名)。

相關問題