Difference between revisions of "PopUpListsLong"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 55: Line 55:
 
Set-up: Create an array in which questions alternate with answers. Answers may be any string.
 
Set-up: Create an array in which questions alternate with answers. Answers may be any string.
 
For true or false questions, this is the only part of the example that needs to be modified.
 
For true or false questions, this is the only part of the example that needs to be modified.
We added a randomized element to the question about pi. Do not use the names $n, $j, or $jj
+
We added a randomized element to the question about pi. Do not use the names $numq, $qn, or $qns
 
to avoid conflicts with the code below.
 
to avoid conflicts with the code below.
 
</p>
 
</p>
Line 79: Line 79:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
$n = scalar(@questions_and_answers)/2;
+
$numq = scalar(@questions_and_answers)/2;
@shuffle = shuffle($n);
+
@shuffle = shuffle($numq);
 
@abc = (a..z);
 
@abc = (a..z);
   
for $jj (0..$n-1) {
+
for $qn (0..$numq-1) {
$j = $shuffle[$jj];
+
my $qns = $shuffle[$qn];
 
BEGIN_TEXT
 
BEGIN_TEXT
@abc[$jj].
+
@abc[$qn].
 
\{ pop_up_list(['?', @choices]) \} $SPACE
 
\{ pop_up_list(['?', @choices]) \} $SPACE
$questions_and_answers[2*$j+1]
+
$questions_and_answers[2*$qns+1]
 
$BR$BR
 
$BR$BR
 
END_TEXT
 
END_TEXT
ANS(str_cmp($questions_and_answers[2*$j], filters=>["trim_whitespace","compress_whitespace"]));
+
ANS(str_cmp($questions_and_answers[2*$qns], filters=>["trim_whitespace","compress_whitespace"]));
 
}
 
}
   

Revision as of 21:56, 23 October 2009

Pop-Up Lists For Many Questions With Common Answers: PG Code Snippet

Often you may have many questions that have the same set of answers, such as a list of true or false questions, or a list of multiple choice questions in which the choices are the same for each question. This PG code allows takes the hassle out of creating such a PG problem by requiring only that you modify an array of questions and answers, the instructions for the student, and the list of choices for every question. To prevent students from being able to copy each other's answers verbatim, we shuffle the order of the questions. It is also possible to include random elements into each question (but not each answer, since the list of answers is a list of strings and needs to be the same for all questions).

This code snippet shows the PG code to create lengthy pop-up lists in a WeBWorK 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.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
  "PG.pl",
  "PGstandard.pl",
  "PGcourse.pl",
  "PGchoicemacros.pl",
);

TEXT(beginproblem);

Initialization: We need to include the PGchoicemacros.pl macro file.

$a = random(1,5,1);

@questions_and_answers = (
"True",  "Larry is one of the three stooges.",
"False", "Gandalf is one of the three stooges.",
"False", "\(\pi + $a\) is rational.",
);

@choices = ("True", "False");

Set-up: Create an array in which questions alternate with answers. Answers may be any string. For true or false questions, this is the only part of the example that needs to be modified. We added a randomized element to the question about pi. Do not use the names $numq, $qn, or $qns to avoid conflicts with the code below.

BEGIN_TEXT

Are the following statements true or false? 
$BR$BR

END_TEXT

Instructions and Question for all parts: You may modify the instructions if you want. You should explicitly say what all of the possible answers are so that students who have a pdf or a paper copy can work directly from it.

$numq = scalar(@questions_and_answers)/2;
@shuffle = shuffle($numq);
@abc = (a..z);

for $qn (0..$numq-1) {
my $qns = $shuffle[$qn];
BEGIN_TEXT
@abc[$qn]. 
\{ pop_up_list(['?', @choices]) \} $SPACE 
$questions_and_answers[2*$qns+1]
$BR$BR
END_TEXT
ANS(str_cmp($questions_and_answers[2*$qns], filters=>["trim_whitespace","compress_whitespace"]));
}

ENDDOCUMENT(); 

Main Text and Answer Evaluation: You should not need to modify this section at all. We shuffle the order of the questions and use a for loop to generate one question and one answer evaluator for each pair in the array @questions_and_answers.

Problem Techniques Index