Skip to content

Instantly share code, notes, and snippets.

@mbeltagy
Last active October 22, 2016 13:16
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/404a8005438ad4add75a9ac53881a8ed to your computer and use it in GitHub Desktop.
Save mbeltagy/404a8005438ad4add75a9ac53881a8ed to your computer and use it in GitHub Desktop.
Interactive Integer problem generator for kids
function int_prob_gen(n)
starting_time=time()
s=0 #successes
pm(x)=x>0? "+":""
for i=1:n
x=rand(-10:10)
y=rand(-10:10)
op=rand(["+","-"])
right_ans="($x)$op($y)"|>parse|>eval
parse_correct=false
while !parse_correct
print("($(pm(x))$x) $op ($(pm(y))$y) = ")
answer_string=readline(STDIN)
answer=typemax(Int)
try
answer=parse(Int,answer_string)
catch
println("Invalid Input! Please try again")
continue
end
parse_correct=true
if answer==right_ans
s+=1
println(rand(["Nice going!","Good Job","You are doing Great","Fantastic","You Rock!"]))
else
println("The correct answer was $right_ans, but you entered $answer.\nBetter luck next time!")
end
end
end
println("You got $s out of $n correct!")
elapsed_time=time()-starting_time
mins=div(elapsed_time,60)|>Int
secs=round(Int, elapsed_time%60)
println("It took $mins minutes and $secs to complete the exercise")
end
#Main
int_prob_gen(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment