This is my code for a multiple choice quiz (MCQ) with difficulty and question options. It uses a namedtuple
for the questions, and puts them in an array, and uses a difficulty parameter to decide how many options are displayed. This is so that I can have 3 difficulty options: one which outputs 2, one 3, and one outputting 4.
from collections import namedtuple
Question = namedtuple("Question", "question answer choices correct")
maths_questions = [Question("What is 1 + 1", "1 + 1 is 2", ["1", "2", "3", "4"], {"b", "2"}),
Question("What is 2 + 3", "2 + 3 is 5", ["5", "4", "2", "1"], {"a", "5"})]
music_questions = [Question("Who sung Gangsta's paradise?", "Coolio", ["1", "2", "Coolio", "4"], {"c", "coolio", "3"}),
Question("What is DANK", "Weed is DANK", ["5", "4", "DANK", "1"], {"c", "dank", "3"})]
ansdict = []
def quiz(questions, difficulty):
score = 0
for question in questions:
global ansdict; ansdict = question.choices
print(question.question) # PRINTS OUT QUESTION
output(difficulty, ansdict)
users_answer = input().lower()
if users_answer in question.correct: # checks whether users input is in the answer tuple
print("Correct")
score += 1
else:
print("Incorrect", question.answer) # if incorrect outputs correct answer
print("{} out of {}".format(score, len(questions)))
def output(difficulty, dicti):
for i in range(difficulty):
print(dicti[i])
quiz(music_questions, 4)