I am trying to make a quiz app in python and want to display the question and options separately can someone help me?
Question:
questions = [
{
"num": 1,
"question": "What does XML stand for?",
"answer": "eXtensible Markup Language",
"options": [
"eXtensible Markup Language",
"eXecutable Multiple Language",
"eXTra Multi-Program Language",
"eXamine Multiple Language"
]
},
{
"num": 2,
"question": "Who invented C# Language?",
"answer": "Anders Hejlsberg",
"options": [
"Bjarne Stroustrup",
"Anders Hejlsberg",
"Charles Babbage",
"James Gosling"
]
},
]
i want to display the first question then user will enter the ans then it will go to next question
so i want help in displaying the question then it should display the next question
Answers:
Well, lets see what you have.
questions: list[dict[str, int | str | list[str]]]
You should implement a function that gets an argument of that type, and that prints it as you want.
def ask(q: list[dict[str, int | str | list[str]]]) -> None:
...
For example it could be similar to the following:
def ask(q: list[dict[str, int | str | list[str]]]) -> None:
print(q["question"])
for i in q["options"]:
print(f"- {i}")
question_num = 1
while question_num < 2:
for question in questions:
print(questions[question_num - 1]["question"])
choice = questions[question_num - 1]["options"]
print(choice)
answer = input(" ")
question_num += 1
You use the while loop to continue to show the question and the for loop to iterate through the list with dictionaries and the variables are in the case, to cross-check the answers the user gives with the correct answer.
I hope this will help.
questions = [
{
"num": 1,
"question": "What does XML stand for?",
"answer": "eXtensible Markup Language",
"options": [
"eXtensible Markup Language",
"eXecutable Multiple Language",
"eXTra Multi-Program Language",
"eXamine Multiple Language"
]
},
{
"num": 2,
"question": "Who invented C# Language?",
"answer": "Anders Hejlsberg",
"options": [
"Bjarne Stroustrup",
"Anders Hejlsberg",
"Charles Babbage",
"James Gosling"
]
},
]
i want to display the first question then user will enter the ans then it will go to next question
so i want help in displaying the question then it should display the next question
Well, lets see what you have.
questions: list[dict[str, int | str | list[str]]]
You should implement a function that gets an argument of that type, and that prints it as you want.
def ask(q: list[dict[str, int | str | list[str]]]) -> None:
...
For example it could be similar to the following:
def ask(q: list[dict[str, int | str | list[str]]]) -> None:
print(q["question"])
for i in q["options"]:
print(f"- {i}")
question_num = 1
while question_num < 2:
for question in questions:
print(questions[question_num - 1]["question"])
choice = questions[question_num - 1]["options"]
print(choice)
answer = input(" ")
question_num += 1
You use the while loop to continue to show the question and the for loop to iterate through the list with dictionaries and the variables are in the case, to cross-check the answers the user gives with the correct answer.
I hope this will help.