What started off as a few silly lists of terms has lead to a little script that could help someone learn a little basic math or other applications which may need random math problems. The script generates two random numbers with a random operator (+ - * /) for someone to answer to the nearest integer (int). It also uses the specific terminology for the type of answer required: sum, difference, product, or quotient. So, it may not do a great deal, but I was told that it could be a nice math tutor, so I decided to share it with everyone and get some general feedback. If you have an expansion or a tweak, please let me know, I am always open for new ideas of what to do with little things like this I have lying around my hard drive.

I have to thank clinton for his help with the functions in the hash and a few other tweaks. I also have ambrus to thank for reminding me about the whole divide by zero thing which is why there are no zeros in the script and for the correct term for the solution to a division problem. :)

#!/usr/bin/perl use strict; use warnings; local $\ = "\n"; my %math = ( 'addition' => { 'result' => 'sum', 'operator' => '+', 'function' => sub {my ($i,$j) = @_; return $i + $j}, }, 'subtraction' => { 'result' => 'difference', 'operator' => '-', 'function' => sub {my ($i,$j) = @_; return $i - $j}, }, 'multiplication' => { 'result' => 'product', 'operator' => '*', 'function' => sub {my ($i,$j) = @_; return $i * $j}, }, 'division' => { 'result' => 'quotient', 'operator' => '/', 'function' => sub {my ($i,$j) = @_; return $i / $j}, }, ); my $operation = (keys %math)[rand keys %math]; sub random_number { return (1..5)[rand 5]; } my $random_number_1 = random_number(); my $random_number_2 = random_number(); sub math_answer { my ($answer) = @_; if ($answer eq "hint") { return qq($random_number_1 $math{$operation}{'operator'} $random_n +umber_2); my $user_answer = <>; answer($user_answer); } elsif ($answer == int($math{$operation}{'function'}->($random_number +_1,$random_number_2))) { return "True"; } else { return "False"; } } print qq(What is the $math{$operation}{'result'} of $random_number_1 a +nd $random_number_2 to the nearest integer? (type "hint" to show the +equation)); my $user_answer = <>; chomp $user_answer; print math_answer($user_answer);
Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re: A little math
by jwkrahn (Abbot) on Feb 21, 2011 at 03:34 UTC
    sub random_number { return (1..5)[rand 5]; }

    Or just:

    sub random_number { return 1 + int rand 5; }



    answer($user_answer);

    There is no  answer sub in your code.

      Thanks for catching the problem with the answer versus math_answer when it should recurse. You are also right about the rand, I overcomplicated it.

      Have a cookie and a very nice day!
      Lady Aleena
Re: A little math
by ambrus (Abbot) on Feb 21, 2011 at 15:52 UTC

    When I type "hint", the program shows the question in a symbolic form, but then doesn't prompt me for an answer again. Does this mean that asking for a hint means I gave up on the question? Also, I'd appreciate if, when I give a wrong answer, the program didn't only tell me it's wrong, but also what the correct answer is.

      Thanks ambrus for finding the problem with "hint". For some reason I don't understand at the moment, using return in the subroutine causes hint to not wait for a user response. I changed it to print, and it now waits for a user response. Also, I added a chance to try again and the correct answer when the user answer is false the second time.

      #!/usr/bin/perl use strict; use warnings; use feature qw(say); local $\ = "\n"; my %math = ( 'addition' => { 'result' => 'sum', 'operator' => '+', 'function' => sub {my ($i,$j) = @_; return $i + $j}, }, 'subtraction' => { 'result' => 'difference', 'operator' => '-', 'function' => sub {my ($i,$j) = @_; return $i - $j}, }, 'multiplication' => { 'result' => 'product', 'operator' => '*', 'function' => sub {my ($i,$j) = @_; return $i * $j}, }, 'division' => { 'result' => 'quotient', 'operator' => '/', 'function' => sub {my ($i,$j) = @_; return $i / $j}, }, ); my $operation = (keys %math)[rand keys %math]; sub random_number { return 1 + int(rand(7)); } my $random_number_1 = random_number(); my $random_number_2 = random_number(); sub math_answer { my ($answer,$loop) = @_; my $result = int($math{$operation}{'function'}->($random_number_1,$r +andom_number_2)); my $equation = qq($random_number_1 $math{$operation}{'operator'} $ra +ndom_number_2); if ($answer eq "hint") { print $equation; my $user_answer = <>; math_answer($user_answer,1); } elsif ($answer == $result) { print "True"; } else { if ($loop == 1) { print "False, why don't you try again? The equation is $equation +."; my $user_answer = <>; math_answer($user_answer,++$loop); } else { print "False, the answer is $result."; } } } print qq(What is the $math{$operation}{'result'} of $random_number_1 a +nd $random_number_2 to the nearest integer? (type "hint" to show the +equation)); my $user_answer = <>; chomp $user_answer; math_answer($user_answer,1);
      Have a cookie and a very nice day!
      Lady Aleena