## WeBWorK Problems

### popup hints after each try

by Jean-Philippe JP -
Number of replies: 4

Hello,

I am trying to figure out how to have popup hints after each try. Is it possible at all ? For example, if the student gets the right anwser the first time arround then no hints popup. But if the student has 3 trys to get the problem right and after the first try he gets it wrong then a hint appears. The same thing would happen if a second try is needed execpt that the hint is different from the first one.

Thank you so much,

Jean-Philippe

### Re: popup hints after each try

by Davide Cervone -
You could use a custom grader to make something like this happen. For example,
    DOCUMENT();        # This should be the first executable line in the problem.

"PGstandard.pl",
"MathObjects.pl",
"PGcourse.pl",
);

TEXT(beginproblem);

Context("Numeric");

BEGIN_TEXT
The ratio of the circumference of a circle to its diameter is: \{ans_rule(10)\}
END_TEXT

ANS(pi->cmp);

@hints = (
"It is a famous number.",
"It is represented by a greek letter.",
"It is better than cake!",
);

my ($result,$state) = avg_problem_grader(@_);
if ($state->{num_of_correct_ans} == 0) { my$n = $state->{num_of_incorrect_ans} - 1;$n = $#hints if$n > $#hints; if ($n >= 0) {
$result->{msg} .= '</i><p><b>Note:</b> <i>' if$result->{msg};
$result->{msg} .=$hints[$n]; } } ($result,$state); }); ENDDOCUMENT(); # This should be the last executable line in the problem.  Here, the hints are stored in an array (@hints) and the grader displays the proper one, depending on the number of answers that are incorrect so far. The last hint will be repeated if he student gets the problem wrong more times than there are hints. (You could make the last hint be something like "There are no more hints" if you don't want the last hint shown more than once.) When the student gets the problem right, no more hints will be shown (even if he or she submits additional wrong answers). The hint will be preceded by the word "Note", but unfortunately, there is no way to prevent that, as it is added by the page layout engine and can't be overridden. It would be possible to use CSS style commands to style the hint text, like make it red or not italic, for example. Note that this grader calls avg_problem_grader to do the work of grading the problem, which gives partial credit when one part is right in a multi-answer question. You could substitute std_problem_grader if you would prefer the answer to be right only when all the parts are answered correctly. Hope that helps. Davide In reply to Davide Cervone ### Re: popup hints after each try by Jean-Philippe JP - Thank you so much, this is exactly what i needed jp In reply to Jean-Philippe JP ### Re: popup hints after each try by Jean-Philippe JP - Hi, The subroutine works perfectly like you said, the only problem is that it wont get the other hints stored in the hint array. So the same hint keeps appearing over and over. For exemple, on the first try the first hint appears, the second try the first hint appears again and so on. Also, the first hint that popups is somewhat random. It isnt necesseraly the first one that was stored in the hint array, it can be the second or the third. DOCUMENT(); loadMacros("PG.pl", "PGbasicmacros.pl", "PGchoicemacros.pl", "PGanswermacros.pl", "PGauxiliaryFunctions.pl", "PGgraphmacros.pl", "problemRandomize.pl", "PGstandard.pl", "MathObjects.pl", "PGcourse.pl", ); ProblemRandomize(when=>"always",onlyAfterDue=>0);$showPartialCorrectAnswers = 0;

TEXT(beginproblem());

$k1 = list_random(0.001,0.012,0.032,0.017,0.0054,0.029);$d1 = list_random(5,3,12,21,3,11);
$x1 = list_random(0.99,0.89,0.45,0.76,0.21,0.76);$q1 = list_random(19,10,12,1,4,61);
$vcstr = ($x1*$q1)/ ($k1*(1-$x1));$vpfr = ($q1/$k1)*ln(1/(1-$x1)); BEGIN_TEXT Soit la réaction suivante:$PAR

A -> B$PAR s’effectuant dans un réacteur isotherme à une température donnée T. Calculer les volumes nécessaires afin d'atteindre une conversion X de l'espèce A de $$x1$$*100% dans un réacteur piston et parfaitement agité. Le débit molaire est de $$d1$$ mol/h et le taux de réaction est:$PAR

- rA = kCa $PAR $$k1$$ s-1$PAR
Le débit volumique entrant est de $$q1$$ dm3/h.$PAR CSRT=\{ans_rule(40)\}litres$PAR
PFR=\{ans_rule(40)\}litres$PAR END_TEXT$ans = $vcstr; &ANS(std_num_cmp($ans));

$ans =$vpfr;
&ANS(std_num_cmp($ans)); @hints = ( "Vous devez seulement utiliser une formule pour arriver à la réponse pour le volume du PFR et une autre pour le volume du CSTR.", "Avez-vous penser aux formules de design", "Il n'y a plus d'autre indice!!!", ); install_problem_grader(sub { my ($result,$state) = avg_problem_grader(@_); if ($state->{num_of_correct_ans} == 0) {
my $n =$state->{num_of_incorrect_ans} - 1;
$n =$#hints if $n >$#hints;
if ($n >= 0) {$result->{msg} .= '</i><p><b>Note:</b> <i>' if $result->{msg};$result->{msg} .= $hints[$n];
}
}
($result,$state);
});

ENDDOCUMENT();

Thank you so much,

JP

### Re: popup hints after each try

by Davide Cervone -
The num_of_correct_ans and num_of_incorrect_ans values are only maintained properly when the problem is part of an actual homework set. If you are using it through the Library Browser, then your answers are not stored, and neither is the number right or wrong, so you should always see the first hint when you submit an answer. So you should add the problem to an actual problem set when you are testing it, even when you are still working on it.

But when you do this, that means once you have submitted answers, they will stay recorded, so you can't go back to earlier hints, even if you have changed the problem file. In order to overcome that (for testing purposes) you can add a test for a special "reset" answer in the grader so that you can reset the problem back to its initial values.

For example:

    install_problem_grader(sub {
my ($result,$state) = avg_problem_grader(@_);
if ($inputs_ref->{AnSwEr1} eq 'reset') {$state->{num_of_correct_ans} = 0;
$state->{num_of_incorrect_ans} = 0;$state->{recorded_score} = 0;
$result->{score} = 0; } elsif ($state->{num_of_correct_ans} == 0) {
my $n =$state->{num_of_incorrect_ans} - 1;
$n =$#hints if $n >$#hints;
if ($n >= 0) {$result->{msg} .= '</i><p><b>Note:</b> <i>' if $result->{msg};$result->{msg} .= $hints[$n];
}
}
($result,$state);
});

Here, the first answer blank is checked to see if you have entered the word "reset", and if so, the problem is reset so that is appears like you are trying it for the first time.

See if that resolves your issue. (You will, of course, want to remove the reset code when you are done editing the problem.)

Davide