2015-02-11 112 views
0

此代碼適用於我正在處理的項目;我顯然試圖調用一個字符串。該程序旨在打印來自SQLite數據庫的測驗分數;方法1,這是我遇到的問題,應該給所有學生的正確課程和難度級別的最新記錄。數據庫看起來像這樣;Python:「str」對象不可調用

CREATE TABLE Scores ( [Key] INTEGER, Score INTEGER NOT NULL, PupilName TEXT NOT NULL, Difficulty BOOLEAN NOT NULL, ClassNumber VARCHAR, DateTime DATETIME );

和代碼(與調試的奇數位)這樣的:

import sqlite3 
 

 
#import collections 
 
print ("Welcome to the Arithmetic Challenge high score table.") 
 
def MainLoop(): 
 
    DisplayType = input ("Please enter: 1 for highest scores by student in alphabetical order; 2 for highest scores, highest to lowest; 3 for average scores, highest to lowest. ") 
 
    Difficulty = input ("Enter difficulty level.") 
 
    ClassNumber = input ("Enter class number.") #Input of variables 
 
    conn = sqlite3.connect("QuizStorage") 
 
    c = conn.cursor()#Connects 
 
    c.execute("SELECT * FROM Scores")        
 
    PupilsEligible = c.fetchall()#Finds pupils 
 
    #count = collections.defaultdict(int) #? 
 
    #print (count) 
 
    print (PupilsEligible) 
 
    DifficultyInt = int(Difficulty) 
 
    if DisplayType == "1": 
 
     print (DifficultyInt) 
 
     c.execute("SELECT * FROM Scores WHERE Difficulty = (?) AND ClassNumber = (?) ORDER BY # DESC LIMIT 1 ORDER BY PupilName"(DifficultyInt, ClassNumber)) 
 
     data = c.fetchall() 
 
     print (data) 
 
    elif DisplayType == "2": 
 
     c.execute("SELECT * , MAX(Score) FROM Scores WHERE Difficulty = (?) AND ClassNumber = (?) GROUP BY PupilName"(DifficultyInt, ClassNumber)) 
 
     data = c.fetchall() 
 
     print (data) 
 
    elif DisplayType == "3": 
 
     c.execute("SELECT * , AVG(Score) FROM Scores WHERE Difficulty = (?) AND ClassNumber = (?) GROUP BY PupilName"(DifficultyInt, ClassNumber)) 
 
     data = c.fetchall() 
 
     print (data) 
 

 
    else: 
 
     print ("That's not a valid answer.") 
 
    for row in c: 
 
     print (row) 
 
    conn.close() 
 
    MainLoop() 
 
MainLoop()

請注意,該代碼具有較早嘗試是天堂」的各種遺物t還沒有被剪掉。對不起,如果這使得它不清楚。 當我嘗試運行它,我得到這個:

Welcome to the Arithmetic Challenge high score table. 
 
Please enter: 1 for highest scores by student in alphabetical order; 2 for highest scores, highest to lowest; 3 for average scores, highest to lowest. 1 
 
Enter difficulty level.2 
 
Enter class number.3 
 
[('Alice Mann',), ('Fred Pratt',), ('John Smith',), ('Karen James',)] 
 
2 
 
Traceback (most recent call last): 
 
    File "H:\My Documents\CA 2\ACHighScore 0.5.5.py", line 37, in <module> 
 
    MainLoop() 
 
    File "H:\My Documents\CA 2\ACHighScore 0.5.5.py", line 19, in MainLoop 
 
    c.execute("SELECT * FROM Scores WHERE Difficulty = (?) AND ClassNumber = (?) ORDER BY # DESC LIMIT 1 ORDER BY PupilName"(DifficultyInt, ClassNumber)) 
 
TypeError: 'str' object is not callable

我已經看過了其他類似的錯誤,並已離開毫無收穫。這個錯誤信息究竟意味着什麼,以及如何避免?

+3

您有'c.execute(「某個字符串」(數據))''。 Python認爲你正試圖調用一個字符串作爲函數。 – georg 2015-02-11 09:59:45

+0

...在這裏它應該是'c.execute(「一些字符串,其中blah =(?)和foo =(?)」,(x1,x2))'把這些值放在問號位置。你錯過了一個逗號。 – Spacedman 2015-02-11 10:03:41

回答

2

的問題是以下行(如錯誤消息說):c.execute("SELECT * ...
這是因爲當你做

"it's a string"(arg1, arg2)) 
      ^^^^^^^ 

你正在嘗試做呼叫的字符串。您可能錯過了逗號:"it's a string", (arg1, arg2)
請注意,您在代碼中有多個類似的錯誤