I have been trying to write a program that would randomly select a question from a list of questions and then display the question itself. The information is stored in a .json
file.
Here is my current code:
import json
import random
with open("C:\\LearningArabic\\LiblibArriby\\Lessons\\L1_Letters\\L1_Letters.json", "r", encoding = "utf-8-sig") as read_file:
data = json.load(read_file)
t = data["letters"]["part one"]["meem"]["questions"]
print(t) #This prints all of the necessary information. Not actually necessary but it helps me know that this section of code is working
print("")
print(t.get(random.choice(("question"))))
print("")
Eventually I'll build it out more but right now this is just to get the idea working.
Here is my .json
file:
{"letters":
{"part one": {
"meem": {
"questions": {
"question": "What sound does 'م' make?",
"arabic": "م",
"transliteration": "m",
"english": "m",
"answer": "m",
"wronganswer" : ["a", "b", "kh", "ta", "da", "du",
"ee", "yeh", "he", "ha", "l", "n", "f"]},
"questions": {
"question": "What letters are in the word 'وز'?",
"arabic": "موز",
"transliteration": "mooz",
"english": "banana",
"answer": "م و ز",
"wronganswer": [
"ه و ز",
"ه و ر",
"م ف ز",
"م و ر",
"ه ف ر"]},
"questions": {
"question": "What word do the letters 'ه ر م' make",
"arabic": "ه ر م",
"transliteration": "haram",
"english": "pyramid",
"answer": "هرم",
"wronganswer": [
"مرم",
"هزم",
"موم",
"مرع",
"هبم"]}
}
}}
}
When I run the code the I get the following:
{'question': "What word do the letters 'ه ر م' make", 'arabic': 'ه ر م', 'transliteration': 'haram', 'english': 'pyramid', 'answer': 'هرم', 'wronganswer': ['مرم', 'هزم', 'موم', 'مرع', 'هبم']}
None
Main Question:
When I print(t)
I think I should be getting a printout of all of the questions. Instead, I'm only getting a printout of the last question. And when I try and print a random question, I get the result of "None" which I don't understand. My lists are all populated, why is python unable to find anything? My main goal is to learn how to select random values from a json file - if my json file format needs to be updated please let me know.