2
\$\begingroup\$

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)
toolic
14.6k5 gold badges29 silver badges204 bronze badges
asked Sep 21, 2017 at 20:32
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Overview

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.

Layout

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?",
 "Coolio",
 ["1", "2", "Coolio", "4"],
 {"c", "coolio", "3"}
 ),

Documentation

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

Simpler

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

Comments

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.

answered Apr 15, 2024 at 11:23
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.