Difference between revisions of "StringsInContext"

From WeBWorK_wiki
Jump to navigation Jump to search
(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...)
 
(11 intermediate revisions by 3 users not shown)
Line 16: Line 16:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context()-&gt;strings-&gt;add(none=&gt;{});
+
Context()-&gt;strings-&gt;add(none=&gt;{});
   
# or, if we wanted a case-sensitive string,
+
# or, if we wanted a case-sensitive string,
# we would instead use
+
# we would instead use
# Context()-&gt;strings-&gt;add(
+
# Context()-&gt;strings-&gt;add(none=&gt;{caseSensitive=&gt;1});
# none=&gt;{caseSensitive=&gt;1});
 
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 28: Line 28:
 
</p>
 
</p>
 
<pre>
 
<pre>
Context()-&gt;strings-&gt;add(none=&gt;{},
+
Context()-&gt;strings-&gt;add(none=&gt;{},N=&gt;{alias=&gt;"none"});
N=&gt;{alias=&gt;"none"});
 
 
</pre>
 
</pre>
 
<p>
 
<p>
Line 35: Line 35:
 
<p>
 
<p>
 
By default, strings are case-insensitive. To make them case sensitive, include this as an option in the Context call.
 
By default, strings are case-insensitive. To make them case sensitive, include this as an option in the Context call.
  +
</p>
  +
<p> There are some shortcuts available if you need to add many allowable strings all at once.
  +
See [http://webwork.maa.org/pod/pg/macros/parserAutoStrings.html parserAutoStrings.pl]
 
</p>
 
</p>
 
</td>
 
</td>
Line 64: Line 67:
 
<p>
 
<p>
 
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 <code>ANS(&nbsp;Compute(2)-&gt;cmp()&nbsp;);</code>; then the correct answer would be 2, and the string "none" would be marked wrong if that were given by a student.
 
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 <code>ANS(&nbsp;Compute(2)-&gt;cmp()&nbsp;);</code>; then the correct answer would be 2, and the string "none" would be marked wrong if that were given by a student.
  +
</p>
  +
<p>
  +
If the answer is either a MathObject String or Real, then no error messages are generated when a student submits a string (already added to the context) or a real number. However, if the answer is a MathObject String or Formula (or some other type), to prevent error messages when students submit answers you need to explicitly tell the answer checker what type of answer to expect using <code>cmp(typeMatch=>Formula("x"))</code>. For example:
  +
<pre>
  +
Context("Numeric");
  +
$i = random(0,1,1);
  +
if ( $i == 0 ){
  +
$answer = Compute("none");
  +
} else {
  +
$answer = Compute("2 * x");
  +
}
  +
  +
# Main text section omitted
  +
  +
ANS( $answer -> cmp(typeMatch=>Formula("x")) );
  +
</pre>
  +
Note that typeMatch must be supplied on the <code>cmp( )</code> call, and cannot be attached to the object itself.
  +
</p>
  +
<p>
  +
It is possible to suppress type warnings entirely using
  +
<pre>
  +
ANS( $answer -> cmp(showTypeWarnings=>0) );
  +
</pre>
 
</p>
 
</p>
 
</td>
 
</td>
Line 71: Line 97:
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
</p>
 
</p>
  +
  +
[[Category:Problem Techniques]]

Revision as of 17:59, 7 April 2021

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
Context()->strings->add(none=>{});

# 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.,

Context()->strings->add(none=>{},N=>{alias=>"none"});

(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.

There are some shortcuts available if you need to add many allowable strings all at once. See parserAutoStrings.pl

  BEGIN_TEXT
  Enter the positive real value of \(x\) for which
  \(x^2 = -2\):
  $BR
  \(x = \) \{ ans_rule(15) \}
  $BR
  ${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.

If the answer is either a MathObject String or Real, then no error messages are generated when a student submits a string (already added to the context) or a real number. However, if the answer is a MathObject String or Formula (or some other type), to prevent error messages when students submit answers you need to explicitly tell the answer checker what type of answer to expect using cmp(typeMatch=>Formula("x")). For example:

Context("Numeric");
$i = random(0,1,1);
if ( $i == 0 ){
 $answer = Compute("none");
} else {
 $answer = Compute("2 * x");
}

#  Main text section omitted

ANS( $answer -> cmp(typeMatch=>Formula("x")) );

Note that typeMatch must be supplied on the cmp( ) call, and cannot be attached to the object itself.

It is possible to suppress type warnings entirely using

ANS( $answer -> cmp(showTypeWarnings=>0) );

Problem Techniques Index