# The Structure of a Webwork Problem

WeBWorK problems consist of 4 or 5 parts: loading macros, randomizing parameters, input/output, answer-checking, and sometimes solutions. WeBWorK problems are written in PG, a derivative of perl; you do NOT need to know perl to write/edit problems!

Here is an example problem with explanation.

### Loading macros

## Date('January 2010')
## Institution('UC Davis')
## TitleText1('Math 16 for UC Davis')
## EditionText1('1')
## AuthorText1('Larson Edwards')
## Section1('4.1')
## Problem1('15')

DOCUMENT();

loadMacros("PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl");

The pound sign (#) denotes a comment. The first 7 lines of this section merely provide information for instructors.

The problem itself begins with

DOCUMENT();

The next line loads macros which translate commands written in the PG language into perl.

### Randomizing parameters

$a = random(2,5,1); do {$b = random(2,4,1); } until ($b !=$a);
$c = random(2,4,1);$d = ($a)**($b);

The dollar sign ($) denotes variables in perl. In this example, a, b, c, and d are variables. The first line sets a to be a random integer between 2 and 5. The second line sets b to be a random integer between 2 and 4, not equal to a. The third line sets c to be a random number between 2 and 4. The last line sets d equal to a^b. Note the use of a double asterisk instead of a caret: while WeBWorK is robust when it comes to accepting student input, it is rather finicky when it comes to creating problems. See http://webwork.maa.org/wiki/Basic_Perl_syntax for more details. http://hobbes.la.asu.edu/courses/webwork-help/randomizers.html contains a thorough description of more ways to randomize parameters. ### Input/output TEXT(beginproblem());$showPartialCorrectAnswers = 1;

BEGIN_TEXT
Solve for $$x$$: $\left(\frac{1}{a}\right)^{c x}=d$
$PAR NOTE: Completely simplify your answer!$PAR
$$x =$$ \{ans_rule(20)\}

END_TEXT

In this section we display the problem to the student and ask the student to enter an answer. Everything the student sees is contained between BEGIN_TEXT and END_TEXT.

Text and math which the student sees are written in LaTeX. However: since the dollar sign ($) is already used to denote variables, you cannot use it to denote LaTeX math mode! Instead, use $$[formula]$$ for in-line display (i.e., instead of single dollar signs), and use $[formula$ for equation mode (i.e., instead of double dollar signs). Note that these commands also work in regular LaTeX. $PAR

denotes a new paragraph.

\{ans_rule(n)\}

creates an answer field into which the student may type an answer. The parameter n controls the length of the field.

### Answer-checking

$ans = -($b)/($c); ANS(num_cmp($ans, mode=>'strict'));

ENDDOCUMENT();        

We first create a new variable called ans and set it equal to the correct answer. We then use the num_cmp subroutine to compare the variable ans to whatever the student entered into the answer field. In this case, strict mode indicates that the student must simplify his/her answer; WeBWorK will not do any sort of simplification.

Note also that, as in the randomization section, we are required to do all our calculations using perl syntax. In this case it looks the same as standard math syntax.

http://hobbes.la.asu.edu/courses/webwork-help/answers.html contains a thorough description of more answer-checking subroutines to check lists of numbers, intervals, functions, etc., as well as other options which can be invoked.

the_structure_of_a_webwork_problem.txt · Last modified: 2011/03/30 12:48 by rthomas