Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python Multiple Choice Quiz Generator (Python 3)

I was studying for an exam and wanted to make a tool that functioned a little bit differently than quizlet.
This code will read in questions and multiple choice options then quiz you on them in a random order.
It will also go over the questions that you missed.
I made it last week and it is surprisingly helpful. I plan to add fill in the blank options and some more customization in the future.
Tell me what you think:
Spoiler (Click to View)

You would read in a file like this:
Spoiler (Click to View)
The '.' marks a new question
The '~' marks the correct answer

It is not much but I figured it may help someone out.
1) You should not be using some strange proprietary format. You should use CSV's.
Code
What color is a firetruck?,Red~,Black,Green,Yellow
etc...
2) You should not be trying to re-invent the wheel.
3) You should be using pandas to actually read/sort these types of applications.

PHP Code

<?php 
import numpy 
as np
import pandas 
as pd

try:
    
input = raw_input
except NameError
:
    
pass

quiz 
= pd.read_csv("test.txt")
quiz = quiz.reindex(np.random.permutation(quiz.index))  # randomize the dataframe
quiz.reset_index(drop=True, inplace=True)  # reset the indexes so they don't fuck me up.
correctAnswers = 0
totalQuestions 
= 0

for i in range(len(quiz.Question)):
    
question = quiz.Question[i]
    
answers = [quiz.Answer1[i], quiz.Answer2[i], quiz.Answer3[i], quiz.Answer4[i]]
    
correctAnswer = ""
    
for j, answer in enumerate(answers):
        if 
answer[-1] == "~":
            
answers[j] = answers[j][:-1]
            
correctAnswer = answers[j]
    if 
correctAnswer:
        
answers = dict(zip(list(np.random.permutation(["a", "b", "c", "d"])), answers))  # randomize answers, too
        
print("{}) {}\nA) {a}\nB) {b}\nC) {c}\nD) {d}".format(i+1, question, **answers))
        
choice = input("> ")
        if 
answers[choice.lower()] == correctAnswer:
            
correctAnswers += 1
        totalQuestions 
+= 1

print("Your Score: {}/{}".format(correctAnswers, totalQuestions)) 

Your file should look like (note: the first line header is VERY important):
Code
Question,Answer1,Answer2,Answer3,Answer4
What color is a firetruck?,Red~,Black,Green,Yellow
What color is snow?,Black,White~,Blue,Red
What flies in the air?,Dove~,Penguin,Tree,Dog
What is not a normal household pet?,Fish,Dog,Cat,Giraffe~
What grows on trees?,Money,Cows,Apples~,Strawberries
What shape is a tire?,Square,Triangle,Circle~,Rectangle
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
The more I post my code on here, the more I realize how much I have to learn.
Thanks for taking the time to critique/rewrite
(Mar 2, 2016 07:06 PM)DipDuo™ Wrote:  The more I post my code on here, the more I realize how much I have to learn.
Thanks for taking the time to critique/rewrite

It's all good. String manipulation is a very useful thing to know, but you should also utilize readily available and well-tested/well-supported frameworks (like pandas and numpy) when possible instead of re-inventing the wheel. I'm not going to lie, I do the same thing fairly often.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
I have a lot to learn xD
I like looking around in someone's code :p

I'll try this out :D
(Mar 3, 2016 09:50 AM)bet4r00t Wrote:  I have a lot to learn xD
I like looking around in someone's code :p

I'll try this out :D

I re-wrote it to support fill in the blank questions as well.
I also have a 'question formatter' program that will format questions to be read by the quizzer.
New Quizzer thing:
Spoiler (Click to View)

Sample questions:
Spoiler (Click to View)

Question formatter:
Spoiler (Click to View)

Goodies will be happy to know that I used some of his advice.
I wanted to stay away from using a non-default library though
This is actually better than quizlet, I did not believe it would be at first.
it's basic, but very cool ^^ thank you for posting. I'll be sure to use this.
[Image: 5BgQiIm.gif]

(Mar 12, 2016 03:59 PM)labroca Wrote:  This is actually better than quizlet, I did not believe it would be at first.

Glad you like it. I honestly use it all the time to study for tests. it really comes in handy when I am trying to study from old multiple choice quizzes.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[C#] by john ronaldsadJul 20, 2022 06:49 PM 4 238 Jul 20, 2022 06:49 PM
Last Post: john ronaldsad
[C] by Russian bearNov 25, 2020 11:02 AM 2 241 Nov 25, 2020 11:02 AM
Last Post: G.P.D.