2014-12-22 85 views
1

我正在爲您的帳戶系統上的此Python Credits工作,但似乎我的最後一個for循環未運行,導致未定義time_set。有誰知道爲什麼我的最後一個for循環沒有運行。出於安全原因已刪除數據庫詳細信息。這裏是我的代碼:MySQLDb上的名稱錯誤

from time import sleep 
import MySQLdb 
db = MySQLdb.connect(host="", # your host, usually localhost 
        user="", # your username 
         passwd="", # your password 
         db="") # name of the data base 
cursor = db.cursor()#Defines a cursor allowing me to execute SQL commands 
def timer(time, _print=False): 
    if time == 0: 
     cursor.execute("SELECT * FROM Accounts WHERE Username='{0}';".format(name)) 
     cursor.execute("UPDATE Accounts SET Credits = 0 WHERE Username = '{1}';".format(creds, name)) 
     return '' 
    else: 
     if _print: print time 
     sleep(1) 
     return timer(time - 1, _print) 
name = raw_input("What username would you like to add credits to?:") 
cursor.execute("SELECT * FROM Accounts WHERE Username='{0}';".format(name)) 
creds = input("How many credits would you like to add to the account?") 
for test in cursor.fetchall(): 
    creds = creds + test[3] 
cursor.execute("UPDATE Accounts SET Credits = {0} WHERE Username = '{1}';".format(creds, name)) 
for row in cursor.fetchall(): 
    time_set = creds * 60 
    print time_set 
timer(time_set, True) 

回答

0

我似乎已經找到了問題。我意識到第二個for循環是完全沒有必要的,因爲我只是將分數計算爲分鐘。所以我刪除

for row in cursor.fetchall(): 
    time_set = creds * 60 
    print time_set 

time_set = creds * 60 
取代了它
1

看來,你正試圖從一個UPDATE查詢,這將讓你一事無成,因爲更新不返回任何行拉行。