From WeBWorK_wiki
Revision as of 18:02, 7 November 2009 by Pearson (talk | contribs)
Jump to navigation Jump to search

Multiple Choice Problems: PG Code Snippet

This code snippet shows the essential PG code to include multiple-choice questions in a problem. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Note that in this example we use old-style multiple choice answer objects. The new-style MathObjects have a multiple choice object as well, but its behavior is sufficiently different than that suggested here that is not documented here.

Problem Techniques Index

PG problem file Explanation

In the initialization section of the file we need to include in our list of loaded macro files.

$mc = new_multiple_choice();
$mc->qa("What is your favorite color?", "blue");
$mc->makeLast("none of the above");

In the problem set-up, we create a new multiple choice object with new_multiple_choice, and then store the question and correct answer with the qa method. Other answers are specified as a list of arguments to the extra method. To force an answer (either a new extra answer, or the correct answer) to appear last in the list of options, use the makeLast method. All other answers will be scrambled when the multiple choice problem is shown to students.

\{ $mc->print_q() \}
\{ $mc->print_a() \}

In the text section we print the question and answers.

$showPartialCorrectAnswers = 0;

ANS( radio_cmp( $mc->correct_ans() ) );

Use the standard problem grader to give credit only if all answers are correct, and do not give feedback on partial correct answers. Otherwise, students can use the feedback or the partial credit received to guess and check if their answers are correct. We grade the problem with radio_cmp.

Problem Techniques Index