Skip to content

Instantly share code, notes, and snippets.

@mbeltagy
Last active September 23, 2016 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbeltagy/30c5e8b511067753e75c1998a770e7b2 to your computer and use it in GitHub Desktop.
Save mbeltagy/30c5e8b511067753e75c1998a770e7b2 to your computer and use it in GitHub Desktop.
Interactive Integer problem generator for kids
import random
def pm(x):
if x>0:
return "+"
else:
return ""
def int_prob_gen(n):
s=0
for i in xrange(n):
x=random.randrange(-10,11)
y=random.randrange(-10,11)
op=["+","-"][random.randrange(2)]
eval_string="({0}){1}({2})".format(x,op,y)
right_ans=eval(eval_string)
parse_correct=False
while parse_correct==False:
answer_string= raw_input("({0}{1}){2}({3}{4}) = ".format(pm(x),x,op,pm(y),y))
answer=123232324
try:
answer=int(answer_string)
except:
print "Invalid Input! Please try again"
continue
parse_correct=True
if answer==right_ans:
s+=1
print ["Nice going!","Good Job","You are doing Great","Fantastic","You Rock!"][random.randrange(5)]
else:
print "The correct answer was {0}, but you entered {1}.\nBetter luck next time!".format(right_ans,answer)
print "You got {0} out of {1} correct!".format(s,n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment