Last active
September 23, 2016 22:14
-
-
Save mbeltagy/30c5e8b511067753e75c1998a770e7b2 to your computer and use it in GitHub Desktop.
Interactive Integer problem generator for kids
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 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