I am trying to create a quiz application. I have the module which asks the questions, shows the 4 possible answers in a listbox and then you click a button to fetch the result and check if true.
My issue is trying to get the data type pulled from the sqlite db interpreted correctly. No matter what I try it comes out as a list, so I convert it to type:dict, but then the rest of the code does not interpret it in the same way as if I manually define the dict type. How can I restructure either the data pull to come out in the correct format, or change the conversion function so that the dict type is showing correctly?
For the record the current interpretation jumbles the order and removes some of the elements of the question/answer matrix.
from tkinter import *
from random import randint
import sqlite3
import random
from string import ascii_lowercase
SentenceFrame = Toplevel()
#Connect to a Database
conn=sqlite3.connect('Question_Bank.db')
c = conn.cursor()
records=[]
#Query into Table
records = c.execute("SELECT Question, Correct_Answer, Option1 ,Option2 ,Option3 FROM Question_Bank").fetchall()
#commit changes
conn.commit()
#close connection
conn.close()
How_it_works = {
"First US State?": [
"Delaware",
"Ohio",
"Texas",
"Maryland"
],
"Fastest car?": [
"Ferrari",
"Mercedes",
"BMW",
"Tesla"
],
}
print(How_it_works)
print(type(How_it_works))
print((len(How_it_works)))
def Convert(a):
it = iter(a)
res_dct = dict(zip(it, it))
return res_dct
print(records)
print(len(records))
print(type(records))
QUESTIONS=Convert(records)
print(QUESTIONS)
print(type(QUESTIONS))
print((len(QUESTIONS)))
my_listbox=Listbox(SentenceFrame)
my_listbox.grid(row=6, column=1)
QuestionLabel = Label(SentenceFrame, text="", font=("Helvetica", 20))
QuestionLabel.grid(row=3, column=1)
num_questions = len(QUESTIONS)
questions = random.sample(list(QUESTIONS.items()), k=num_questions)
num_correct = 0
for num, (question, alternatives) in enumerate(questions, start=1):
QuestionLabel.config(text=question)
correct_answer = alternatives[0]
labeled_alternatives = dict(
zip(ascii_lowercase, random.sample(alternatives, k=len(alternatives)))
)
my_list=[labeled_alternatives]
print(my_list)
for k, v in labeled_alternatives.items():
my_listbox.insert(END, k ": " v)
The result I get is How_it_Works: {'First US State?': ['Delaware', 'Ohio', 'Texas', 'Maryland'], 'Fastest car?': ['Ferrari', 'Mercedes', 'BMW', 'Tesla']} <class 'dict'> 2
How the database pull returns the data:
[('First US State?', 'Delaware', 'Ohio', 'Texas', 'Maryland'), ('Fastest car?', 'Ferrari', 'Mercedes', 'BMW', 'Tesla')] 2 <class 'list'>
How the data shows up after conversion:
{('First US State?', 'Delaware', 'Ohio', 'Texas', 'Maryland'): ('Fastest car?', 'Ferrari', 'Mercedes', 'BMW', 'Tesla')} <class 'dict'> 1
How my quiz app interprets it:
[{'a': 'Ferrari', 'b': 'BMW', 'c': 'Fastest car?', 'd': 'Mercedes', 'e': 'Tesla'}]
CodePudding user response:
I don't quite follow what you're doing at the moment. I propose something like this:
questions = []
for r in records:
questions.append({
"question": r[0],
"answer": r[1],
"options": list(r[1:])
})
for question in questions:
correct_answer = question["answer"]
print(question["question"])
labelled = zip(ascii_lowercase, random.sample(question["options"], len(question["options"])))
for label, option in labelled:
print(label ": " option)
I've replaced the tkinter stuff with print
so it was easier to work with but it shouldn't be too hard to change it back. It starts by creating a dictionary for each record which has the question, answer and options. I've then used a for loop to show all the questions, you'd probably want to show them one at a time with a button or something instead. The labelling of questions is similar to how you did it before but I've iterated the pairs instead of making a dictionary.