Application Program: Test of ability with arithmetic english demo

From OLPC

Revision as of 01:29, 15 March 2010 by 164.67.235.148 (Talk)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search


#!/bin/python
# mathsQuiz.py
# date: 09/08/2006 version 1.0
# author: Mesar Hameed <olpcmathsquiz.20.nospamthx@xoxy.net>
# License: Attribution-ShareAlike 2.5 or later

# a quick mockup program to illustrate/bounce ideas for starters to
# Application Program: Test of ability with arithmetic - OLPC
# http://wiki.laptop.org/go/Application_Program:_Test_of_ability_with_arithmetic


# what needs doing:
# level 9 is missing
# think about possible ways of creating translations in a plugable fashion
# either as command line argument or as compile time option
# tk gui, make it all pretty and sugary
# test on sugar platform


import sys
import random
import timing

nq=10 #number of questions per round

def addition(lvl,nq,minval,maxval):
	result = []
	for curr in range(nq):
		a,b = random.randrange(minval,maxval),random.randrange(minval,maxval)
		c=a+b
		result.append(a), result.append('+')
		result.append(b), result.append(c)
	result.insert(0,lvl)
	return result


def subtraction(lvl,nq,minval,maxval,simplified=False):
	result = []
	for curr in range(nq):
		a,b = random.randrange(minval,maxval),random.randrange(minval,maxval)
		if simplified and b>a:
			a,b=b,a
		c=a-b
		result.append(a), result.append('-')
		result.append(b), result.append(c)
	result.insert(0,lvl)
	return result


def multiplicationTable(lvl,nq,maxval,division=False):
	result = []
	for curr in range(nq):
		a,b = random.randrange(0,maxval),random.randrange(0,maxval)
		c=a*b
		if division:
                    if c==0 and a==0:
                        a,b,c=c,b,a
                    if c==0 and b==0:
                        a,b,c=b,a,c
                    result.append(c), result.append('/')
                    result.append(b), result.append(a)
		else:
				result.append(a), result.append('*')
				result.append(b), result.append(c)
	result.insert(0,lvl)
	return result

def playround(questions):
	name=questions.pop(0)
	print "playing the maths quiz, level ", name
	nq=len(questions)/4
	timing.start()
	while len(questions) != 0:
		qid="("+str(1+nq-len(questions)/4)+"/"+str(nq)+") "
		q =qid + str(questions[0])+ " " + str(questions[1]) + " " + str(questions[2]) + " = "
		inputhandler(q,questions[3])
		del questions[:4]
	#end loop
	timing.finish()
	print "Well done!, you completed the level in ",round((timing.milli()/1000.0),2), "seconds"

def inputhandler (prompt,expectedAnswer="",attempts=3):
    counter = ans=0
    while counter < attempts:
        try:
            ans=raw_input(prompt)
            if ans == "exit" :
                verifyExit()
                ans="0"
                counter-=1
            if str(ans) in str(expectedAnswer) and str(ans) != "":
                return ans
        except ValueError:
            counter+=1
            continue
        counter+=1
    print "the correct answer is ",expectedAnswer
    return 0

def verifyExit():
    try:
        quit=raw_input("Are you sure you wish to exit? [y/n]: ")
        if quit == "y":
            print "thankyou for playing the maths quiz."            
            sys.exit()
    except:
        print "goodbye"
        sys.exit()
        

def welcome():
    print "Welcome to OLPC maths quiz!\thttp://www.laptop.org"
    print "Have a play, and see how far you can get.\n\n"
    print "1, level 1: basic addition (to 20)"
    print "2, level 2: basic subtraction (from 20)"
    print "3, level 3: intermediate addition (to 200)"
    print "4, level 4: intermediate subtraction (from 100)"
    print "5, level 5: addition of negative numbers"
    print "6, level 6: mixture of negative and posative numbers"
    print "7, level 7: multiplication table. ( to 12*12)"
    print "8, level 8: basic division"
    print "Type exit at any point to quit."
    
    while True:
        print "there are 8 levels, which level do you want to play?"
        lvl=inputhandler("1 -- 8? ",range(1,9))
        if lvl == "1":
            questions=addition(1,nq,0,10)
        elif lvl == "2":
            questions=subtraction(2,nq,0,20,1)
        elif lvl == "3":
            questions=addition(3,nq,0,50)    
        elif lvl == "4":
            questions=subtraction(4,nq,0,100,1)
        elif lvl == "5":
            questions=addition(5,nq,-100,0)
        elif lvl == "6":
            questions=subtraction(6,nq,0,100,0)
        elif lvl == "7":
            questions=multiplicationTable(7,nq,12)
        elif lvl == "8":
            questions=multiplicationTable(8,nq,12,1) #this actually does divisions within the multiplication table.
        playround(questions)
    

welcome()

Personal tools
  • Log in
  • Login with OpenID
About OLPC
About the laptop
About the tablet
Projects
OLPC wiki
Toolbox