WeBWorK Problems

RadioButtons fails if answer is 0

RadioButtons fails if answer is 0

by Edwin Flórez -
Number of replies: 2
Hello, in the following simple problem when I choose 0 like correct answer, the RaddioButtons doesn't get the real answer, takes 2, the first one on the array. But if I choose any one different to 0, it works. Please help me to find why, really I can't figure it out. 

For now, I am putting 0 the fist element in the array, in that way works.

**********************************************************
#  DESCRIPTION
#  My Problem
#  ENDDESCRIPTION

## DBsubject('')
## DBchapter('')
## DBsection('')
## KEYWORDS('')

DOCUMENT();   
     
loadMacros(
   "PGstandard.pl",
   "MathObjects.pl",
   "parserRadioButtons.pl",
);

$mc = RadioButtons(
  ["2", "1", "0", "-1", "-2"],
  "0", # correct answer
);

TEXT(beginproblem());
Context()->texStrings;
BEGIN_TEXT
\{ $mc->buttons() \}
END_TEXT
Context()->normalStrings;

$showPartialCorrectAnswers = 0;
ANS( $mc->cmp() );

Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
Coming soon.
END_SOLUTION
Context()->normalStrings;

ENDDOCUMENT();
*********************************************************

In reply to Edwin Flórez

Re: RadioButtons fails if answer is 0

by Davide Cervone -
Here's what's happening. The second parameter to RadioButtons() gives the correct answer, and that can be given in of two ways: either as the index of the correct answer, or as the correct answer string itself. In your case, because the answers are themselves numbers, it is ambiguous which of those you mean. There is (as far as I know) no way to distinguish a numeric value from string literal containing that string value, so "0" and 0 can't be distinguished by the MathObejcts library.

It turns out that the correct-answer-as-index is checked first, so in your example, the correct answer is the first one in your array (the one with index 0), namely 2. So to have the correct answer be "0", the third one in the array (which has index 2), you would need to use

    RadioButtons(
        ["-2","-1","0","1","2"],
        2 # index 2 is the correct answer of "0"
     );
It's a bit awkward, but that's how it currently is implemented.

Hope that helps.

In reply to Davide Cervone

Re: RadioButtons fails if answer is 0

by Edwin Flórez -
Upps, helps a lot. It was so simple. I did not about the two possibilities for the answer parameter. Where I can find that kind of information? or may I check the MathObjects library? 

Thank you.