Show DevBest [Python] PyQuiz - (Maths, Tabulate and SQLite)

Ethereal

Neurotic Male
May 18, 2013
494
330
Here's a program I made that makes the user take a mathematics quiz, records their name and class number, then logs their score to a database via MSQlite. It then gives the user the ability to output the scores (in sexy tables I might add) of anyone who has taken the quiz in these selectable formats: average score, highest score in ascending order and name of everyone who has taken the quiz alphabetically. The program was designed to be taken by pupils in three different classes.

Anyone running the program needs to install Tabulate, as it is the programs one and only dependency:
Code:
pip install tabulate

Link to sqlite database file full of users:
Link to private Gist (the code):
Location of the module I used to create the sexy tables:

This program should provide a good understanding of SQLite in Python and a little bit of how maths work too. Sorry that the program is so
elongated. :'>

Here's a little example of the averaging selection:
ZQBwOzC.png
 
Last edited:

Ollie

Member
Jan 7, 2013
72
11
lmfao this was literally my Computer Science coursework for Programming, except for the fact that we were not allowed to use dependencies so it was long-winded. Looks ok, personally I would have done it in a class and just connected to the db with __init__ and closed with __del__ rather than connecting each time someone picks an option.
 

Moogly

Member
May 6, 2012
80
48
clean dood

a project like this would be really nice with a gui like tkinter (which is not a thrid party module)

Aside from using tkinter to make it more approachable, anytime you repeat the same code like a SQL query, you could parametize it and use format as necessary:
Code:
query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum)
Then instead of:
Code:
with con:
if classnum == '1':
   cur = con.cursor()
   cur.execute('''INSERT INTO Class1(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))
   con.commit()
elif classnum == '2':
   cur = con.cursor()
   cur.execute('''INSERT INTO Class2(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))
   con.commit()
elif classnum == '3':
   cur = con.cursor()
   cur.execute('''INSERT INTO Class3(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))
   con.commit()
You could do:
Code:
# with con line goes before this block:
cur = con.cursor()
query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum)
cur.execute(query_str, (firstname,secondname,correct))
con.commit()

You would get rid of 9 lines of code and accomplish the same. If you ever find yourself repeating yourself in code, chances are you can find a quicker way in less lines of code to accomplish the same. It may take a while to grasp sometimes, but the more willing you are to fix up your code the better you will be and closer you will become to being a Pythonista. Anyway: great job, maybe for your next challenge try adding Tkinter as an interface, it's not as hard as it sounds. I was able to use tkinter with little to no Python experience before.
 

Ethereal

Neurotic Male
May 18, 2013
494
330
Aside from using tkinter to make it more approachable, anytime you repeat the same code like a SQL query, you could parametize it and use format as necessary:
Code:
query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum)
Then instead of:
Code:
with con:
if classnum == '1':
   cur = con.cursor()
   cur.execute('''INSERT INTO Class1(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))
   con.commit()
elif classnum == '2':
   cur = con.cursor()
   cur.execute('''INSERT INTO Class2(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))
   con.commit()
elif classnum == '3':
   cur = con.cursor()
   cur.execute('''INSERT INTO Class3(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))
   con.commit()
You could do:
Code:
# with con line goes before this block:
cur = con.cursor()
query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum)
cur.execute(query_str, (firstname,secondname,correct))
con.commit()

You would get rid of 9 lines of code and accomplish the same. If you ever find yourself repeating yourself in code, chances are you can find a quicker way in less lines of code to accomplish the same. It may take a while to grasp sometimes, but the more willing you are to fix up your code the better you will be and closer you will become to being a Pythonista. Anyway: great job, maybe for your next challenge try adding Tkinter as an interface, it's not as hard as it sounds. I was able to use tkinter with little to no Python experience before.
Very true, though I did make this program a couple of years ago. These days i'd probably just slap a bunch of the functionality into separate modules, simply because of the sheer repetition and how annoying it would be to adapt its functionality in the future. DRY programming was quite the eye-opener for me. :p
 

Moogly

Member
May 6, 2012
80
48
Very true, though I did make this program a couple of years ago. These days i'd probably just slap a bunch of the functionality into separate modules, simply because of the sheer repetition and how annoying it would be to adapt its functionality in the future. DRY programming was quite the eye-opener for me. :p
Aw shnap didn't realize how old the thread is LOL my bad.

Edit:

Good to know you kept at Python, it's a great language.
 

Users who are viewing this thread

Top