import random # # constant specifying number of questions, min and max operands for questions # NUM_QUESTIONS = 10 MIN_OPERAND = 1 MAX_OPERAND = 10 # # randOperator: Randomly picks + or * # # Parameters: (none) # # Returns: either "+" or "*" # def randOperator(): i = random.randrange(0,2) if (i == 0): return "+" else: return "*" # # createQuestion: Creates a new math question involving small integers, and # the * and + operators # # Parameters: (none) # # Returns: # operand1, operator, operand2, correct_answer # def createQuestion(): operand1 = random.randrange(MIN_OPERAND,MAX_OPERAND) operator = randOperator() operand2 = random.randrange(MIN_OPERAND,MAX_OPERAND) if (operator == "+"): correct_answer = operand1 + operand2 else: correct_answer = operand1 * operand2 return (operand1, operator, operand2, correct_answer) def main(): print "-------------------------------" print "" print "This will be a %d question quiz" % NUM_QUESTIONS print "" print "-------------------------------" correct_count = 0 for i in range(1, NUM_QUESTIONS+1): (a, op, b, correct_answer) = createQuestion() print "" print "Question",i," -- What is",a,op,b,"?" answer = input() if (answer == correct_answer): print "Correct!!!!" correct_count = correct_count + 1 else: print "Nope (answer was %d): better luck next time" % correct_answer print "" print "" print "-------------------------------" print "" if (correct_count > 5): print "Congratulations, you got %d out of %d!" % (correct_count,NUM_QUESTIONS) else: print "You got %d out of %d. I think you need some more practice :(" % (correct_count,NUM_QUESTIONS) print "" print "-------------------------------" main()