0

This code is for a project I'm working on; I'm apparently trying to call a string. The program is designed to print quiz scores from an SQLite database; method 1, which is what I'm having problems with, should give the most recent records of all students of the right class and difficulty level. The database looks like this;

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

and the code (with the odd bit of debug) like this:

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

Please note that this code has various relics of earlier attempts that haven't yet been pruned off. Sorry if this makes it unclear. When I try to run it, I get this :

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

I've looked at the other similar errors and been left none the wiser. What exactly does this error message mean, and how can it be avoided?

2
  • 3
    You have c.execute("some string"(data)). Python thinks you're trying to call a string as a function. Commented Feb 11, 2015 at 9:59
  • ...where it should be c.execute("some string where blah=(?) and foo=(?) ", (x1, x2)) to put the values in the question mark places. You missed a comma. Commented Feb 11, 2015 at 10:03

1 Answer 1

2

The problem is in the following line (like the error message says): c.execute("SELECT * ...
It's because when you do

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

you are trying do call a string. You've probably missed a comma there: "it's a string", (arg1, arg2)
Note that you have multiple similar errors in your code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.