From WeBWorK_wiki
Revision as of 14:20, 14 February 2008 by Glarose (talk | contribs) (New page: <h2>Allowing String Answers: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to allow stude...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Allowing String Answers: PG Code Snippet

This code snippet shows the essential PG code to allow students to enter text as alternate answers in a question. 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

  # or, if we wanted a case-sensitive string,
  #    we would instead use
  # Context()->strings->add(
  #    none=>{caseSensitive=>1});

No changes are necessary to the tagging and description or initialization sections of the problem file. In the problem set-up section, we add the strings that are to be allowed in answers to the Context. Note that the add call has the form string=>{options}. The most common use of options is to allow "aliases"---strings that are marked the same as others, e.g.,


(Which would allow "N" to be used instead of "none".)

By default, strings are case-insensitive. To make them case sensitive, include this as an option in the Context call.

  Enter the positive real value of \(x\) for which
  \(x^2 = -2\):
  \(x = \) \{ ans_rule(15) \}
  ${BITALIC}(Enter ${BBOLD}none$EBOLD if there
  are no values that satisfy the equation.)$EITALIC

In the text section of the problem file, we include the problem as expected. It's usually a good idea to include some indication of what strings are expected or allowed in the answer.

  ANS( Compute("none")->cmp() );

And in the answer and solution section of the file, we evaluate the answer as expected. Note that if the problem had a solution, we could have used something like ANS( Compute(2)->cmp() );; then the correct answer would be 2, and the string "none" would be marked wrong if that were given by a student.

Problem Techniques Index