Home > Mobile >  How to corectly query for type "dict"?
How to corectly query for type "dict"?

Time:11-15

For my quiz application I have a module which asks questions, shows the 4 possible answers in a listbox, then you click a button to fetch result and check if true.

Data type is interpreted incorrectly as a list, I convert to type:dict but then the code does not interpret it the same as if manually defined. How can I restructure either query or conversion function so dict type is correct? Current interpretation jumbles the order and removes some elements :

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) 

Result :

How_it_Works:
{'First US State?': ['Delaware', 'Ohio', 'Texas', 'Maryland'], 'Fastest car?': ['Ferrari', 'Mercedes', 'BMW', 'Tesla']}
<class 'dict'>
2

How database returns the data:

[('First US State?', 'Delaware', 'Ohio', 'Texas', 'Maryland'), ('Fastest car?', 'Ferrari', 'Mercedes', 'BMW', 'Tesla')]
2
<class 'list'>

Data after conversion:

{('First US State?', 'Delaware', 'Ohio', 'Texas', 'Maryland'): ('Fastest car?', 'Ferrari', 'Mercedes', 'BMW', 'Tesla')} <class 'dict'> 1

How 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.

  • Related