Python:" str"对象不可调用

时间:2015-02-11 09:57:36

标签: python sqlite

此代码适用于我正在处理的项目;我显然试图打电话给一个字符串。该程序旨在打印SQLite数据库中的测验分数;方法1,这是我遇到的问题,应该提供所有正确的班级和难度级别的学生的最新记录。数据库看起来像这样;

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

请注意,此代码包含尚未删除的早期尝试的各种遗留物。对不起,如果这不清楚。 当我尝试运行它时,我明白了:

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

我已经看过其他类似的错误而且没有更聪明。这个错误消息到底意味着什么,以及如何避免?

1 个答案:

答案 0 :(得分:2)

问题出现在以下行中(如错误消息所示):c.execute("SELECT * ...
这是因为你做的时候

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

你正在尝试调用一个字符串。您可能在那里错过了一个逗号:"it's a string", (arg1, arg2)
请注意,代码中有多个类似的错误