Skip to content

Instantly share code, notes, and snippets.

@emwdx
Created September 1, 2012 05:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emwdx/3564547 to your computer and use it in GitHub Desktop.
Save emwdx/3564547 to your computer and use it in GitHub Desktop.
Quiz question generator with randomized values v0.4
import random,math
class Question:
""" Question class for creating a question for use in a quiz """
def ft(self,text,randints,randdec,randsgnint):
return text.format(randints,randdec,randsgnint)
def __init__(self, question, answer):
self.text = question
self.answer = answer
self.tolerance = 0.01
self.randints = []
self.randdec = []
self.randsgnint = []
for a in range(1,11): #Creates three arrays of whole numbers, random decimals, and random signed integers for use in questions.
self.randints.append(random.randint(1,10))
self.randdec.append(math.sqrt(random.randint(1,100))*random.randint(1,10))
self.randsgnint.append(random.randint(-10,10))
self.text = self.ft(question,self.randints,self.randdec,self.randsgnint) #The function replaces all symbols in the question with the correct number types
self.answer = eval(self.ft(answer,self.randints,self.randdec,self.randsgnint)) #This stores the numerical answer based on the string provided with the answer.
def checkans(self,answer):
if (abs(float(answer) - self.answer)<=self.tolerance):
return True
else:
return False
questionindex = ['','','']
questionindex[0] = ["Convert {1[0]:0.2f} centimeters into meters.","{1[0]}*0.01"]
questionindex[1] = ["What is the length of the line segment with endpoints ({2[1]},{2[2]}) and ({2[3]},{2[4]})?","math.sqrt(({2[3]}-{2[1]})**2 + ({2[4]}-{2[2]})**2)"]
questionindex[2] = ["Solve for x in the equation {2[1]}x - {0[2]} = {2[7]}","({2[7]}+{0[2]})*1./{2[1]}"]
#question = "What is the length of the line segment with endpoints ({2[1]},{2[2]}) and ({2[3]},{2[4]})?"
#answer = "math.sqrt(({2[3]}-{2[1]})**2 + ({2[4]}-{2[2]})**2)"
#question = "Solve for x in the equation {2[1]}x - {0[2]} = {2[7]}"
#answer = "({2[7]}+{0[2]})*1./{2[1]}"
for q in questionindex:
currentQuestion = Question(q[0],q[1])
print currentQuestion.text
print currentQuestion.answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment