mercredi 11 février 2015

Python: "str" object is not callable

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?


Aucun commentaire:

Enregistrer un commentaire