I'm writing this as I have come to have troubles in my python code that shall be a test for school students, I am unable to add a working solution to allow the results from the SQLite database to be printed to the user, furthermore I would like the user to be asked how they would like it formatted e.g. alphabetically or score ascending/descending. Please help me!!!
import random
import sqlite3 as lite
import os
print ("Welcome to the Maths Test")
Score = 0
question = 0
while True:
Name = input("Please enter your name here: ")
if Name.isdigit():
print ("Do Not Enter a Number")
elif Name.isalpha():
break
while True:
class_group = input("Please enter your class group number here: ")
if class_group.isalpha():
print ("Do not enter a number")
elif class_group.isdigit():
break
while question < 10:
operation= random.randint(1,3)
number1 = random.randint(1,25)
number2 = random.randint(1,25)
if operation == 1:
print("What is", number1, "+", number2)
answer = number1 + number2
elif operation ==2:
print("What is", number1, "*", number2)
answer = number1 * number2
else:
print("What is", number1, "-", number2)
answer = number1 - number2
while True:
try:
user_answer = int(input())
except ValueError:
print ("That is not a valid answer")
continue
else:
break
if user_answer == answer:
print("Correct")
Score += 1
else:
print("Incorrect")
question += 1
if question == 10:
print("Well done you scored", Score, "/10")
break
if os.path.isfile('results.db'):
connection = lite.connect('results.db')
else:
connection = lite.connect('results.db')
for x in range (1, 4):
connection.execute('CREATE TABLE %s(Name INTEGER, Score TEXT);'% x)
def Pupil_Results(Name, Score):
table = 'class%s' % class_group
with connection:
cur = connection.cursor()
cur.execute('SELECT * FROM ' + table + ' WHERE name = "%s";' % Name)
rows = cur.fetchall()
if len(rows) > 0:
cur.execute('UPDATE ' + table + ' SET Name = "%i" WHERE Score = "%s";' % (Name, Score))
else:
cur.execute('INSERT INTO ' + table + '(Name, Score) VALUES (?, ?);', (Name, Score))
Pupil_Results(Name, Score)
Aucun commentaire:
Enregistrer un commentaire