I have a daughter in 3rd grade who started the year off with reviews of the addition and subtraction she learned last year. Fortunately, her school is private and doesn’t do any of the dumb Common Core math that I have seen other parents complaining about. My daughter understands the concepts of addition and subtraction and she understands the approach to solving each type of problem, independent of the variables used in the equation. I wanted to give her some more practice so I started by making up random math problems and having her solve them in her head. Since my MacBook and a python book were sitting on the kitchen table while we were doing this I wondered if I could use python’s ability to generate random numbers to help me make up math problems for her. Moreso, I wanted python to add or subtract the numbers it was randomly generating and check them against the answers provided by my daughter. The script below is what I came up with. For me, it was an exercise that served more than one purpose; it helped me help my daughter with her math mojo and it also helped me use python to solve a real problem. And that’s what has me so excited about python; it helps you solve problems.
As python scripts go, this is pretty simple, I know. But it felt good to have a real-life situation arise that caused me to ask, “How can python help me here?”, and to have an answer a little later in the evening.
Here’s the code:
#!/usr/bin/env python
import os
import random
import time
import signal
os.system("clear")
def doAddition(value1, value2):
print ""
print "\t %d" % value1
print "\t+ %d" % value2
print "\t------"
correctAnswer = value1 + value2
answer = int(raw_input("Answer: "))
while answer != correctAnswer:
print """
%d is incorrect.
Check your work and try again.""" % answer
answer = int(raw_input("Answer: "))
else:
print ""
print "Good Job!!! %d is correct!" % correctAnswer
print "-------------------------------------------------------"
time.sleep(3)
os.system('clear')
print "Next practice problem:"
return value1 + value2
def doSubtraction(value1, value2):
if value1 > value2: # Don't want any negative numbers as answers
print ""
print "\t %d" % value1
print "\t- %d" % value2
print "\t------"
correctAnswer = value1 - value2
answer = int(raw_input("Answer: "))
while answer != correctAnswer:
print """
%d is incorrect.
Check your work and try again.""" % answer
answer = int(raw_input("Answer: "))
else:
print ""
print "Good Job!!! %d is correct!" % correctAnswer
print "-------------------------------------------------------"
time.sleep(3)
os.system('clear')
print "Next practice problem:"
return value1 - value2
else: # Don't wany negative numbers
print ""
print "\t %d" % value2
print "\t- %d" % value1
print "\t------"
correctAnswer = value2 - value1
answer = int(raw_input("Answer: "))
while answer != correctAnswer:
print """
%d is incorrect.
Check your work and try again.""" % answer
answer = int(raw_input("Answer: "))
else:
print ""
print "Good Job!!! %d is correct!" % correctAnswer
print "-------------------------------------------------------"
time.sleep(3)
os.system('clear')
print "Next practice problem:"
return value1 - value2
def main():
numSize = int(raw_input("""What size numbers do you want to work with?
1. Tens (Ex: 55, 21, 98)
2. Hundreds (Ex: 121, 452, 871)
3. Thousands (Ex: 1452, 1337, 7731)
Enter an option #: """))
if numSize == 1:
value1 = random.randrange(10,100)
value2 = random.randrange(10,100)
elif numSize == 2:
value1 = random.randrange(100,1000)
value2 = random.randrange(100,1000)
elif numSize == 3:
value1 = random.randrange(1000,10000)
value2 = random.randrange(1000,10000)
desiredMath = int(raw_input("""Do you want to practice addition or subtraction?
1. Addition
2. Subtraction
Enter an option #: """))
global numSize
global desiredMath
global value1
global value2
return
main()
while True:
if desiredMath == 1:
doAddition(value1, value2)
if numSize == 1:
value1 = random.randrange(10,100)
value2 = random.randrange(10,100)
elif numSize == 2:
value1 = random.randrange(100,1000)
value2 = random.randrange(100,1000)
elif numSize == 3:
value1 = random.randrange(1000,10000)
value2 = random.randrange(1000,10000)
elif desiredMath == 2:
doSubtraction(value1, value2)
if numSize == 1:
value1 = random.randrange(10,100)
value2 = random.randrange(10,100)
elif numSize == 2:
value1 = random.randrange(100,1000)
value2 = random.randrange(100,1000)
elif numSize == 3:
value1 = random.randrange(1000,10000)
value2 = random.randrange(1000,10000)
I actually wrote this code a few weeks ago and now, as I am reviewing it for this post I can see that there are many things I need to do to make it better. I originally tried to use signal handlers to have CTRL+C restart the script but I haven’t yet gotten it to work. I also think the while statment could be simplified to not have as much repeating code. I’ll see if I can avoid shiny things in the next days or weeks and come back and clean this up a bit. Despite the problem I see (never mind the problems an experienced python programmer will see!) I am happy to have combined helping my daughter with helping my own knowledge.
Cheers,
Colin Weaver
If you liked this post, please consider sharing it. Thanks!