WeBWorK Problems

Weighted Grader and Radio Buttons

Weighted Grader and Radio Buttons

by Cindy Loten -
Number of replies: 3
My university has webwork 2.15.

I've programmed a question that has a truth table followed by a radio button question with options for interpreting the truth table.  I've used the weighted grader macro to increase the weight of the radio button part of the question, but have left the weights of the truth table entries alone.  Following the example I found in

https://webwork.maa.org/moodle/mod/forum/discuss.php?d=3884

I used the line

[$radio->buttons]* [@ WEIGHTED_ANS( $radio->cmp(), $button_mark ) @]

to change the weight of the radio button.  I'm getting a weird 0 at the end of the buttons.  Below, I've included code for a simpler problem to show the code I've used (no big truth table) and a screen shot to show the weird 0.

## DESCRIPTION

## changing the weight of the radio buttons

## ENDDESCRIPTION

DOCUMENT();

loadMacros(

"PGstandard.pl",

"MathObjects.pl",

"parserRadioButtons.pl",

"weightedGrader.pl",

"PGML.pl",

"PGcourse.pl"

);

TEXT(beginproblem());

############################

# Setup

install_weighted_grader();

$radio = RadioButtons(

["must be first",

["random1",

"random2",

"random3" ],

"must be last"

],

0, # correct answer

labels => "ABC"

);

$button_mark = 5; # weighted for radio buttons, all others weight 1

$entries = 3; # number of entries

$button_pc = sprintf("%0.0f",($button_mark/($entries+$button_mark))*100 );

#$showPartialCorrectAnswers = 0; # don't show which answer are wrong

############################

# Main Text

BEGIN_PGML

Some questions that have weight 1

Enter the number ten

[_]{10}{10}

Enter the number twenty

[_]{20}{20}

Enter the number thirty

[_]{30}{20}

and now the radio button question that has weight [$button_mark].

[$radio->buttons]* [@ WEIGHTED_ANS( $radio->cmp(), $button_mark ) @]

Note: The multiple choice question is worth [$button_pc ]% of your grade.

END_PGML

# If I include answer checker down here,

#don't get weird zero at the end of the radio buttons

#WEIGHTED_ANS( $radio->cmp(), $button_mark ) ;

##########################

# Solution

BEGIN_PGML_SOLUTION

[$radio->correct_ans()]

END_PGML_SOLUTION

ENDDOCUMENT();




In reply to Cindy Loten

Re: Weighted Grader and Radio Buttons

by Glenn Rice -
Change the line in your problem that reads
[$radio->buttons]* [@ WEIGHTED_ANS( $radio->cmp(), $button_mark ) @]
to
[@ WEIGHTED_ANS( $radio->cmp(), $button_mark ); $radio->buttons @]*

What is happening is that the second [@ ... @] evaluates the Perl statement inside, and outputs its result. In this case the return value of the WEIGHTED_ANS method happens to be 0, and so that is what you see.

Another way to fix this would be to change the above line to
[$radio->buttons]* [@ WEIGHTED_ANS( $radio->cmp(), $button_mark ); '' @]
Note that those are two single quotes after the semicolon. Then the output of the Perl inside the [@ ... @] would be the empty string which wouldn't show up.
In reply to Glenn Rice

Re: Weighted Grader and Radio Buttons

by Glenn Rice -
Note that the sprintf rounding method you are using to output the value of the multiple choice part has rounding issues. Before rounding it is 62.5. sprintf rounds that to 62%, but the WeBWorK rounds that to 63%. One way to fix this is the use the PG "round" method instead. For that change
$button_pc = sprintf("%0.0f", ($button_mark / ($entries + $button_mark)) * 100);
to
$button_pc = round(($button_mark / ($entries + $button_mark)) * 100);
Althouh, I am not certain that there is any real value to doing this computation. I would drop the $button_mark, $entries, and $button_pc variables and just use the actual numbers everywhere.
In reply to Glenn Rice

Re: Weighted Grader and Radio Buttons

by Cindy Loten -
[@ WEIGHTED_ANS( $radio->cmp(), $button_mark ); $radio->buttons @]*
fixed my problem! Thanks for the quick answer and the explanation for where the 0 even came from.
I had noticed the rounding error, and it was next on my list of things to sort out.

I had the values $button_mark, $entries, and $button_pc as the actual questions I coded had truth tables numerous entries. This just made it easier for coding lots of problems with similar structures.