Main.py
In my current codes, it just stuck on the first question and not continuing the second questions. I want to make it loop until the last question answered and the loop will break/stop.
def main():
Total = None
with open("test.json", "r") as f:
File = json.load(f)
for i in File["data"].keys():
Total = i # Total of the keys
def do_game():
num = 0
num = 1
if num > int(Total):
print("No more questions are available.")
return False # To break the loop but didn't work at all
name = File["data"][str(num)]["type"]
print("Enter type of filename (#{}) :".format(num))
answer = input("Answer: ")
if answer == name:
print("Correct!")
else:
print("Incorrect!")
while True:
do_game() # I tried to loop this but didn't go well
main()
test.json
{
"questions": {
"1": {
"question": "Video.mp4",
"answer": "mp4"
},
"2": {
"question": "cool.gif",
"answer": "gif"
},
"3": {
"question": "main.py",
"answer": "python"
}
}
}
CodePudding user response:
Try adjusting your loop to:
while True:
result = do_game()
if result == False:
break
CodePudding user response:
You can try this:
def main():
Total = None
with open("test.json", "r") as f:
File = json.load(f)
for i in File["data"].keys():
Total = i # Total of the keys
num = 1
while num <= int(Total):
name = File["data"][str(num)]["type"]
print("Enter type of filename (#{}) :".format(num))
answer = input("Answer: ")
if answer == name:
print("Correct!")
else:
print("Incorrect!")
num = 1
main()
Since your data counting from 1 so we set num = 1
.
Your code simply not run question
1 and 2 (which are first 2 item of your data). It doesn't make any loops through the file.
CodePudding user response:
You need to modify your code. Try this,
import json
def main():
with open("test.json", "r") as f:
File = json.load(f)
total = len(File['questions'])
for i in range(1,total 1):
name = File["questions"][str(i)]["answer"]
print("Enter type of filename (#{}) :".format(i))
answer = input("Answer: ")
print("Correct!" if answer==name else "Incorrect!")
else:
print("No more questions are available.")
return False # To break the loop but didn't work at all
main()
Output:
Enter type of filename (#1) :
Answer: mp4
Correct!
Enter type of filename (#2) :
Answer: m
Incorrect!
Enter type of filename (#3) :
Answer: s
Incorrect!
No more questions are available.
CodePudding user response:
Many little errors in your code.
Here is a functional code with some improvements. They are written as comments in the code.
import json
def do_game(jsonFile): #You need to put this function outside the main
for question in jsonFile["questions"]: # Use a for loop over the json list
filename = question["question"]
print(f"Enter type of filename '{filename}'") #Use F-String
answer = input("Answer: ")
print("Correct!" if answer==question["answer"] else "Incorrect!")
def main():
Total = None
with open("test.json", "r") as f:
jsonFile = json.load(f)
do_game(jsonFile) # Do all the game's code in a function
# It's a good practice to execute the main funciton only if this file is the main file
if __name__ == "__main__":
main()
As you have a list of questions, use a list in your json
{
"questions": [
{
"question": "Video.mp4",
"answer": "mp4"
},
{
"question": "cool.gif",
"answer": "gif"
},
{
"question": "main.py",
"answer": "python"
}
]
}