I am currently making a quiz through python and Tkinter. I’m trying to use txt files, one for my questions and one for my set of answers for the said question, as well as one for explanations for the answers. However, I’m not sure how to implement this into my code. I’m not sure how to retrieve a question from the txt file and represent it as a label while also having the correct set of answers for that question represented as buttons for the user to choose. As well as display the text for the correct explanation for the answer. Is there a simple way to do this? Should I rather use an array instead?
Asked
Active
Viewed 738 times
0
-
Just put them all in one file. Read the file in once into three arrays, one with questions, one with answers, one with explanations. – Tim Roberts Mar 23 '21 at 04:14
-
@TimRoberts Thanks for the reply! But how would I store the individual parts from the same txt file into three different arrays? – Unko Mar 23 '21 at 04:24
-
You could use dictionary in the form of `{question:[id:answer]}` and then you can have a text file with ids and its explanation and search the text file for the id. – Delrius Euphoria Mar 23 '21 at 04:30
-
One common way of storing this sort of data is in JSON format. Here's an example https://opentdb.com/api.php?amount=10. – norie Mar 23 '21 at 04:32
-
In the old days using `awk`, we'd put prefixes on the line. `Q: This is question 1.` / `A: This is answer 1.` / `B: This is answer 2.` / `X: This is a long explanation, possibly spanning multiple lines.` – Tim Roberts Mar 23 '21 at 05:01
-
This should have been closed as needing focus or details/clarity, rather than as a duplicate, because there are many possible ways to interpret **what the code should exactly do**. In general, we don't take "design" questions anyway. – Karl Knechtel Aug 29 '23 at 19:46
1 Answers
0
This should get you started on the idea of how I think you should proceed.
- Store the question and answer and id in a dictionary like:
data = {question:[id,answer]}
# So it would be like
data = {'Who was the first prime minister of india?':[1,'Jawaharlal Nehru'],
'Tallest building in the world':[2,'Burj Khalifa'],
'Largest country in the world':[3,'Russia']}
- Create a file explanation.txt and then store the id and explanation in the form of:
id - explanation
- So the text file for explanation(explnation.txt) would be something like:
1 - Your explanation goes here: Tryst with destiny
2 - UAE
3 - WC 2018
- So then the code for all this together would be something like:
import tkinter as tk
import random
root = tk.Tk()
# Store it here
data = {'Who was the first prime minister of india?':[1,'Jawaharlal Nehru'],
'Tallest building in the world':[2,'Burj Khalifa'],
'Largest country in the world':[3,'Russia']}
score = 0 # Score of correct answers
def get_question():
global id, answer, explanation
exp_label.config(text='') # Clear the previous explanation
question = random.choice(list(data.keys())) # Get the question
item = data[question] # Get the corresponding list
id = item[0] # Get the id from the list
answer = item[1] # Get the answer from the list
explanation = get_explanation(id) # Find the explanation using the id
q_label.config(text=question) # Update the question to this
def submit():
global score
if answer_ent.get().lower() == answer.lower(): # If correct answer
score += 1 # Increase the score by 1
score_label.config(text=f'Score: {score}') # Update the score label
else: # If wrong answer
exp_label.config(text=explanation) # Show the explanation
answer_ent.delete(0,'end') # Clear the entry
def get_explanation(id):
with open('explanation.txt','r') as file: # Open the file
lst = file.readlines() # Read each line and make it a list
for i in lst: # Looping through that list
fetched_id = i.split(' - ')[0] # Split the txt with ' - ' and get the id
if int(fetched_id) == id: # If fetched and our question id are same
explanation = i.split(' - ')[1][:-1] # Get the explanation and trim the \n
return explanation # Return it
q_label = tk.Label(root,font=(0,21))
q_label.grid(row=0,column=0)
answer_ent = tk.Entry(root)
answer_ent.grid(row=1,column=0,pady=10,padx=20)
exp_label = tk.Label(root,font=(0,13))
exp_label.grid(row=3,column=0,pady=10)
score_label = tk.Label(root,text='Score: 0',font=(0,13))
score_label.grid(row=4,column=0)
tk.Button(root,text='Submit',command=submit).grid(row=5,column=0,pady=10)
tk.Button(root,text='Next',command=get_question).grid(row=6,column=0,pady=10)
get_question() # Call the function immediately
root.mainloop()
I have explained the code using comments to make it understandable on-the-go. This is just a small scale example that you can take and expand and more features, like making sure no same question is repeated and so on. This way just seems easy for me using tkinter
.

Delrius Euphoria
- 14,910
- 3
- 15
- 46