Share your repls and programming experiences

← Back to all posts
Quiz creator for Python (v1)
DarshanRajpara1 (13)

This is a Python module that I made which allows you to create a quiz, if you have any suggestions, let me know.

How to get it working

To use this module, create a file called quiz.py, and copy the code below.

class Question:
  def __init__(self, value, a1, a2, a3, a4, cor):
    import random

    self.value = value
    self.answers = ['', a1, a2, a3, a4]
    self.correct = False
    if cor == 'random' or 'rand':
      self.cor = random.randrange(0,100) % 4 + 1
    else:
      self.cor = cor
  
  def ask(self):
    print(self.value,'\n1)',self.answers[1],'\n2)',self.answers[2],'\n3)',self.answers[3],'\n4)',self.answers[4])
    guess = int(input())
    if guess == self.cor:
      return True
    else:
      return False

Then, at the start of your main file type:

import quiz

Using it

To create a new question, use this code:

question = Question('question', 'answer 1', 'answer 2', 'answer 3', 'answer4', correctanswer)

The correctanswer value can also be set to 'rand' if you want a random answer to be correct.


To actually ask a question just type

question.ask()

The ask() function will return true if the user answers correctly, and false if they answer incorrectly.
Below is a simple history quiz.