Joseph Lee
Joseph Lee

Reputation: 3

How to print a table based on an average of multiple scores (SQLite, Python)

I've been working on a program that asks a student to complete 10 mathematical questions, it then logs the users First name, Second name and Score based on their inputs at the beginning into a database using SQLite 3.

I would like the program to print a table based on the average score of all that students attempts at the quiz. For example, these two users average would be that of 8.5: enter image description here

I currently use tabulate 0.7.5 to handle the table method, as done here with the sorting by score:

if sort == "score":
       print("Accessing Database.")
       time.sleep(2)
       print("Accessing Database..")
       time.sleep(2)
       print("***Reading Complete***")
       con = lite.connect('students.db')
       with con:
           cur = con.cursor()
           table = cur.execute("SELECT FirstName, SecondName, Score  FROM Class1 ORDER BY Score DESC")
           print(tabulate(table, headers=["FirstName","SecondName", "Score"]))

I tried using a SQLite parameter to do it, but it just pasted a single average. I'd truly like a separate average that covers every attempt the student has made.

Any help would be much obliged!

Upvotes: 0

Views: 99

Answers (1)

Chris Johnson
Chris Johnson

Reputation: 21976

Something like:

select
    firstname, lastname, avg(score) as score
from
    class1
group by
    firstname, lastname
order by
    avg(score) desc

Let the database engine do the work for you!

Upvotes: 1

Related Questions