Skip to content

Instantly share code, notes, and snippets.

@jacks205
Last active December 17, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacks205/5573956 to your computer and use it in GitHub Desktop.
Save jacks205/5573956 to your computer and use it in GitHub Desktop.
Quiz Handler gives out a quiz to the users, and then calculates the score and other information.
[
{ #question 1
'type' : 'multiple-choice',
'text' :
'Who was the first U.S. President?\n' +
'(1) Abraham Lincoln\n' +
'(2) George Washington\n' +
'(3) John Lennon\n' +
'(4) Franklin Roosevelt',
'answer' : 2
},
{ #question 2
'type' : 'yes/no',
'text' : 'Does the adult platypus have any teeth?',
'answer' : 'No'
},
{ #question 3
'type' : 'multiple-choice',
'text' :
'Who made this quote famous?\n' +
"I do not know if love proves God's\n" +
'existence, or love is God himself.\n' +
'(1) Pope John Paul II\n' +
'(2) Isaac Asimov\n' +
'(3) Ingmar Bergman\n' +
'(4) Rev. Sergey Bulgakov',
'answer' : 3
},
{ #question 4
'type' : 'fill-in',
'text' : 'What is the most populous state in the USA?',
'answer' : 'California'
}
]
[
{ # Question 1
'type' : 'yes/no',
'text' : 'Is Orange the state capital of California?',
'answer' : 'No'
},
{ # Question 2
'type' : 'multiple-choice',
'text' :
'Who made this quote famous?\n' +
'A professor is one who can speak on any subject\n' +
' - for precisely fifty minutes. \n' +
'(1) Norbert Wiener\n' +
'(2) Atanas Radenski\n' +
'(3) George Bush\n' +
'(4) Albert Einstein',
'answer' : 1
},
{ # Question 3
'type' : 'fill-in',
'text' : 'What is the state capital of California?',
'answer' : 'Sacramento'
},
{ # Question 4
'type' : 'multiple-choice',
'text' :
'What city is the state capital of California?\n' +
'(1) Los Angeles\n' +
'(2) San Francisco\n' +
'(3) Sacramento\n' +
'(4) Orange',
'answer' : 3
}
]
# Quiz Handler
import random;
# Ask questions, collect answers, count attempts, and determine marks:
def handleQuiz():
marksTotal = 0; attemptsTotal = 0;
quiz = inputQuiz();
random.shuffle(quiz);
questions = len(quiz);
for question in quiz:
mark, attempts = evaluate(question, limit = 3);
marksTotal = marksTotal + mark;
attemptsTotal = attemptsTotal + attempts;
summarizeQuiz(marksTotal, questions, attemptsTotal);
# Print number of questions and score:
def summarizeQuiz(marksTotal, questions, attemptsTotal):
print 'Number of questions attempted:', questions;
if (questions > 0):
print 'Total attempts:', attemptsTotal;
print 'Total marks:', marksTotal;
decimal = float(marksTotal) / questions;
score = round(100 * decimal);
print 'Score on a 100 points scale:', score;
# Input quiz from a file:
def inputQuiz():
fileName = raw_input('Enter quiz file name: ');
quizFile = open(fileName, 'r');
quizText = quizFile.read();
quizList = eval(quizText);
quizFile.close();
return quizList;
# Display single question:
def displayQuestion(question):
print '...';
print question;
# Evaluate multiple choice question responses up to a limit:
def evaluateMultipleChoice(question, answer, limit):
mark = 0; attempts = 0;
while (attempts < limit):
displayQuestion(question);
choice = raw_input('Enter choice: ');
attempts = attempts + 1;
if (choice == str(answer)):
print 'That is correct!';
mark = 1; break;
else:
print 'Incorrect choice: ', choice;
return mark, attempts;
# Evaluate fill-in question responses up to a limit:
def evaluateFillIn(question, answer, limit):
mark = attempts = 0;
while (attempts < limit):
displayQuestion(question);
response = raw_input('Enter response: ');
attempts = attempts + 1;
if (response == str(answer)):
print 'That is correct!';
mark = 1;
break;
else:
print 'Incorrect response: ', response;
return mark, attempts;
# Evaluate Yes/No question once:
def evaluateYesNo(question, answer):
displayQuestion(question);
response = inputYesNo();
if (response == str(answer)):
print 'That is correct!';
mark = 1;
else:
print 'Incorrect response: ', response;
mark = 0;
attempts = 1;
return mark, attempts;
# Input valid answer for a Yes/No question:
def inputYesNo():
response = raw_input('Enter response [Yes/No]: ');
while (response != 'Yes' and response != 'No'):
print 'Invalid response: ', response;
response = raw_input('Enter response [Yes/No]: ');
return response;
# Evaluate single question:
def evaluate(question, limit):
if (question['type'] == 'multiple-choice'):
return evaluateMultipleChoice(
question['text'], question['answer'], limit);
if (question['type'] == 'yes/no'):
return evaluateYesNo(
question['text'], question['answer']);
if (question['type'] == 'fill-in'):
return evaluateFillIn(
question['text'], question['answer'], limit);
print 'Error: Unknown question type';
return 0, 0; # mark = 0, attempts = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment