Home > Mobile >  I am trying to make a quiz app in python and want to display the question and options separately can
I am trying to make a quiz app in python and want to display the question and options separately can

Time:12-25

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

CodePudding user response:

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}")

CodePudding user response:

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 is 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

  • Related