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
            score += 1
            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):

quiz(music_questions, 4)

1 Answer 1



You've done a good job:

  • Encapsulating code into functions
  • You leveraged code written by others with the import
  • Used meaningful names for the functions and variables

Here are some adjustments for you to consider, mainly for coding style.


I recommend moving the functions to the top, after the import statement. Having them in the middle of the code interrupts the natural flow of the code (from a human readability standpoint).

The code for each Question is a little hard to understand all being on one line. I recommend separating onto multiple lines:

music_questions = [Question
                        "Who sung Gangsta's paradise?",
                        ["1", "2", "Coolio", "4"],
                        {"c", "coolio", "3"}


Add doctrings to describe what the purpose of the code is and what each function does.


There is no need to declare ansdict as global, and this line can be deleted:

ansdict = []

The variable should also be renamed as answer_dict.

The following line:

print("{} out of {}".format(score, len(questions)))

would be simpler using an f string:

print(f"{score} out of {len(questions)}")


There is no need for the comment in the following line because it just repeats what the code is obviously doing:

    print(question.question)  # PRINTS OUT QUESTION

The other comments can be deleted as well.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.