Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dev/pacmath.activity/questionGenerator.py
blob: a6120774b50224614f4a9f7c33878ef1497a5a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /usr/bin/env python
"""
Creates random questions for PacMath
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
"""
from random import randint
import pygame

class questionGenerator:
        """
        A reusable object which is used to generate questions
        @var operation: The operation that the math questions will incorporate
        @var minOp: The minimum allowable value for an operand
        @var maxOp: The maximum allowable value for an operand
        """
	def __init__(self, operation, minOp, maxOp):
        	"""
        	Constructor
                @var operation: The operation that the math questions will incorporate
                @var minOp: The minimum allowable value for an operand
                @var maxOp: The maximum allowable value for an operand
       		"""
		self.minOp = minOp
		self.maxOp = maxOp
		self.operation = operation
	
	def getQuestion(self):
                """
                Returns a single question based on the constraints the generator
                was created with
                @return A tuple contining two strings: ("question", "answer")
                """
		op1 = randint(self.minOp, self.maxOp)
		op2 = randint(self.minOp, self.maxOp)	

		if( self.operation == ['x'] ):
			answer = op1*op2
			return (str(op1)+" x " + str(op2) + " = ?", str(answer))
                if( self.operation == ['+'] ):
                        answer = op1+op2
			return (str(op1)+" + "+ str(op2) + " = ?", str(answer))
                elif( self.operation == ['-'] ):
                        answer = op1-op2
			return (str(op1)+" - "+ str(op2) + " = ?", str(answer))
                elif( self.operation == ['/'] ):
                        answer = op1*op2
			return (str(answer)+" / "+ str(op1) + " = ?", str(op2))
		else:
			return null
	def getQuestionSet(self):
                """
                Returns a set of four questions for use in a round of pacmath, 
                ensures that no questions have duplicate answers.
                Fails and exits if four questions with distinct answers cannot
                be generated from this constraint set (eg minOp = 2, maxOp = 3:
                only 2 * 2, 2 * 3, 3 * 3)
                @return A list with four questions in the form ("question", "answer")
                """
		questions = []
		attempts = 0
		while len(questions) < 4 and attempts < 100:
			toAdd = self.getQuestion()
			notDuplicate = True
			for i in questions:
				if toAdd[1] == i[1]:
					notDuplicate = False
					attempts += 1					
			if notDuplicate:
				questions.append( toAdd )
		
		if attempts >= 100:
			print "Bad question set, not enough questions could be generated"
			print "Minimum Operand: " + str(self.maxOp)
			print "Maximum Operand: " + str(self.minOp)
			print "Operation: " + str(self.operation)
			raise SystemExit
		return questions