Created
December 26, 2010 22:56
-
-
Save kevinburke/755704 to your computer and use it in GitHub Desktop.
test mental math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
def convert_num_to_word(num): | |
'''given a 2 digit number, convert to word''' | |
assert(num <= 99) | |
d = { 1: 'one', 2: 'two', 3:'three', 4:'four', 5:'five', 6:'six', \ | |
7:'seven',8:'eight', 9:'nine', 10 : 'ten', 11 : 'eleven', \ | |
12 : 'twelve', 13: 'thirteen', 14: 'fourteen', \ | |
15 : 'fifteen', 16 : 'sixteen', 17: 'seventeen', \ | |
18 : 'eighteen' , 19 : 'nineteen', 20 : 'twenty', 30: 'thirty', \ | |
40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', \ | |
80: 'eighty', 90: 'ninety'} | |
if d.has_key(num): | |
return d[num] | |
else: | |
#split into tens and ones | |
ones = num % 10 | |
tens = math.floor(num / 10) * 10 | |
return d[tens] + " " + d[ones] | |
assert(convert_num_to_word(93) == "ninety three") | |
assert(convert_num_to_word(12) == "twelve") | |
assert(convert_num_to_word(67) == "sixty seven") | |
def plus(first, second): | |
'''plus''' | |
return first + second | |
def times(first, second): | |
'''times''' | |
return first * second | |
def test_user(func, low, high): | |
'''take a function and range for random numbers, ask user questions with | |
numbers printed on the screen. if user enters "stop" print out the success | |
rate. to do: add in time to completion for each question.''' | |
score = attempted = 0 | |
while True: | |
first_num = random.randint(low,high) | |
second_num = random.randint(low,high) | |
print convert_num_to_word(first_num) + " " + func.__doc__ + " " + \ | |
convert_num_to_word(second_num) + " is:" | |
answer = raw_input() | |
if answer in "stop": | |
print "Correct:\t" + str(score) + "\nAttempted:\t" + str(attempted) + \ | |
"\nPercent:\t" + '{:.1%}'.format(float(score)/attempted) | |
break | |
the_answer = func(first_num, second_num) | |
if int(answer) == the_answer: | |
print "Correct!" | |
score += 1 | |
attempted += 1 | |
else: | |
print "Incorrect! Answer is " + str(the_answer) | |
attempted += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment