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

In reply to Re^2: A little math by Lady_Aleena
in thread A little math by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.