Quiz Mini Project in Python using questions in JSON format

This is the third post in a series of mini-projects for Python where you can learn programming knowledge related to python and implement them as a project. The project consists of 10 questions in JSON format where the code reads the questions from JSON.

To begin the implementation in Python, make sure you have the following tutorials covered:-

Source code for the mini-project of quiz game using python

import json
import os
import time
import random
helpLineUsed = False

def fiftyFifty(question_ansDict):
    keysList = list(question_ansDict.keys())
    fiftyFiftyDict = {'q': question_ansDict['q'],
                      'ca': question_ansDict['ca'],
    # correctAnswerKey = optionMap[question_ansDict['ca']]
    correctAnswerKey = question_ansDict['ca']
    fiftyFiftyDict[correctAnswerKey] = question_ansDict[correctAnswerKey]
    while len(keysList) > 1:
        randomIndex = random.randint(0, len(keysList) - 1)
        fiftyFiftyDict[keysList[0]] = question_ansDict[keysList[0]]
    return fiftyFiftyDict

def showOptionAnswer(question_dict, isFiftyFifty):
    print("\n {questionNo}) {question}".format(questionNo=i + 1, question=question_dict['q']))
    # keyMap = ["a", "b", "c", "d"]
    optionBuilder = ""
    for key, value in sorted(question_dict.items()):
        if key == 'ca' or key == 'q':
        optionBuilder += key + "/"
        print(" <{optionNo}> {option}".format(optionNo=key, option=value))
    if isFiftyFifty:
        answer = input("\n Enter the option ({}) ".format(optionBuilder))
        answer = input("\n Enter the option (a/b/c/d) or h for helpline or q to quit: ")
    return answer

print(" ~~~~~~ WELCOME TO QUIZ ~~~~~")
print("\n ### RULES OF THE GAME ###")
print(" 1. Choose one of the correct option from each question (a/b/c/d)")
print(" 2. h for help")
print(" 3. q to quit")
print("\n ### BEST OF LUCK ###\n")

with open("qa.json", "r") as qa:
    questionSet = qa.read()
    # json saved in the file is jsonArray hence loads() this function converts it into list
    questionsList = json.loads(questionSet)
    rightAnswer = 0
    i = 0
    while i < len(questionsList):
        question_dict = questionsList[i]
        answer = showOptionAnswer(question_dict, False)
        i += 1
        if answer == 'h':
            if helpLineUsed:
                print("<<< Help Line Already Used >>>")
                i -= 1
                helpLineUsed = True
                question_dict = fiftyFifty(question_dict)
                answer = showOptionAnswer(question_dict, True)

        if answer == 'q':
            print("\nGame Over")
            print("You made {} right. Your score is {} ".format(rightAnswer, str(rightAnswer * 10)))

        if question_dict['ca'] == answer:
            print("You predicted right answer")
            rightAnswer += 1

        if not helpLineUsed:
            print("\nYour answer is wrong")
            print("\nCorrect answer is {correctAnswer}".format(correctAnswer=question_dict['ca']))


        print("\n!!!Game Over")
        print("You made {} right. Your score is {} ".format(rightAnswer, str(rightAnswer * 10)))

The output of the above code is:-

You can find the complete code in Github link.

SHARE Quiz Mini Project in Python using questions in JSON format

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *