Add attempt() that checks for the answer properly.

- Now checks if Regexp matches as well as the Answer field.
 - Adds a test question with a Regexp field.
This commit is contained in:
Jonathan Harker 2015-11-19 17:26:02 +13:00
parent e729158718
commit 789a4bb3fc
2 changed files with 9 additions and 2 deletions

View file

@ -7,6 +7,7 @@ See http://moxquizz.de/ for the original implementation in TCL.
from __future__ import unicode_literals, print_function
from io import open
import re
class Question:
@ -163,6 +164,9 @@ class Question:
if 'Tipcycle' in attributes_dict.keys():
self.tipcycle = attributes_dict['Tipcycle']
def attempt(self, answer):
return (self.answer is not None and self.answer.lower() == answer.lower()) or (
self.regexp is not None and re.search(self.regexp, answer, re.IGNORECASE) is not None)
class QuestionBank:
"""
@ -287,7 +291,7 @@ if __name__ == '__main__':
print(q.question)
a = unicode(raw_input('A: '), 'utf8')
#a = input('A: ') # Python 3
if a.lower() == q.answer.lower():
if q.attempt(a):
print("Correct!")
else:
print("Incorrect - the answer is '%s'" % q.answer)

View file

@ -108,4 +108,7 @@ Question: What is the main ingredient of borscht?
Answer: beetroot
Author: DoctorLard
Category: Geography
Question: In what mountain range is Kicking Horse Pass?
Answer: Rocky Mountains
Regexp: Rock(y|ies)