2015-02-11 511 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)
请注意,您在代码中有多个类似的错误