Difference between revisions of "ConstantsInProblems"
m |
(added historical tag and gave updated problem link) |
||
(8 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | <h2>Named Constants in Problems: PG Code Snippet</h2> |
||
+ | {{historical}} |
||
+ | |||
+ | <p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/ConstantsInProblems.html a newer version of this problem]</p> |
||
+ | <h2>Named Numerical Constants (Parameters) in Problems: PG Code Snippet</h2> |
||
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> |
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> |
||
Line 16: | Line 19: | ||
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | + | Context("Numeric"); |
|
− | + | Context()->constants->add(k=>0.01); |
|
+ | |||
+ | # this means that student answers are |
||
+ | # not reduced to the numerical value |
||
+ | # specified in the Context |
||
+ | Context()->flags->set( |
||
+ | formatStudentAnswer=>'parsed' |
||
+ | ); |
||
</pre> |
</pre> |
||
</td> |
</td> |
||
<td style="background-color:#ffffcc;padding:7px;"> |
<td style="background-color:#ffffcc;padding:7px;"> |
||
<p> |
<p> |
||
− | No changes are needed in the tagging and description or initialization sections of the problem file. In the problem set-up section, we add to the Context the constants we're going to use. Here we define a constant k, and assign it a value that will be used when expressions involving k are evaluated. |
+ | No changes are needed in the tagging and description or initialization sections of the problem file. In the problem set-up section, we add to the Context the constants we're going to use. Here we define a constant k, and assign it a value that will be used when expressions involving k are evaluated. Do not set k=1, because if you do, then kx and x/k are equivalent, for example. Obviously, do not set k=0. |
</p> |
</p> |
||
<p> |
<p> |
||
In this case we specified <code>constants->add()</code>, so that the constant k is added to existing constants in the problem. If we had used <code>constants->are()</code>, we would also remove all predefined constants from the Context (in a manner similar to the use of <code>variables->add()</code> and <code>variables->are()</code> when [[VariablesOtherThanX|defining variables]] in a problem. |
In this case we specified <code>constants->add()</code>, so that the constant k is added to existing constants in the problem. If we had used <code>constants->are()</code>, we would also remove all predefined constants from the Context (in a manner similar to the use of <code>variables->add()</code> and <code>variables->are()</code> when [[VariablesOtherThanX|defining variables]] in a problem. |
||
+ | </p> |
||
+ | <p> |
||
+ | One other tweak that we might want to put in here is to reset a |
||
+ | [[Context_flags|Context flag]] so that students' answers are not reduced to numerical |
||
+ | values when they are previewed or submitted. This is done by setting the <code>formatStudentAnswer</code> flag, as shown. |
||
</p> |
</p> |
||
</td> |
</td> |
||
Line 32: | Line 40: | ||
<td style="background-color:#ffdddd;border:black 1px dashed;"> |
<td style="background-color:#ffdddd;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | + | BEGIN_TEXT |
|
− | + | \(f(x) = x-k\) (where \(k>0\) is constant) |
|
− | + | is zero when $BR |
|
− | + | \(x = \)\{ ans_rule(15) \} |
|
− | + | END_TEXT |
|
</pre> |
</pre> |
||
<td style="background-color:#ffcccc;padding:7px;"> |
<td style="background-color:#ffcccc;padding:7px;"> |
||
Line 47: | Line 55: | ||
<td style="background-color:#eeddff;border:black 1px dashed;"> |
<td style="background-color:#eeddff;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | + | ANS( Compute("k")->cmp() ); |
|
</pre> |
</pre> |
||
<td style="background-color:#eeccff;padding:7px;"> |
<td style="background-color:#eeccff;padding:7px;"> |
||
Line 59: | Line 67: | ||
[[IndexOfProblemTechniques|Problem Techniques Index]] |
[[IndexOfProblemTechniques|Problem Techniques Index]] |
||
</p> |
</p> |
||
+ | |||
+ | [[Category:Problem_Techniques]] |
Latest revision as of 08:14, 28 June 2023
This problem has been replaced with a newer version of this problem
Named Numerical Constants (Parameters) in Problems: PG Code Snippet
This code snippet shows the essential PG code to include named constants 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.
PG problem file | Explanation |
---|---|
Context("Numeric"); Context()->constants->add(k=>0.01); # this means that student answers are # not reduced to the numerical value # specified in the Context Context()->flags->set( formatStudentAnswer=>'parsed' ); |
No changes are needed in the tagging and description or initialization sections of the problem file. In the problem set-up section, we add to the Context the constants we're going to use. Here we define a constant k, and assign it a value that will be used when expressions involving k are evaluated. Do not set k=1, because if you do, then kx and x/k are equivalent, for example. Obviously, do not set k=0.
In this case we specified
One other tweak that we might want to put in here is to reset a
Context flag so that students' answers are not reduced to numerical
values when they are previewed or submitted. This is done by setting the |
BEGIN_TEXT \(f(x) = x-k\) (where \(k>0\) is constant) is zero when $BR \(x = \)\{ ans_rule(15) \} END_TEXT |
In the text section of the problem we then ask a question involving the constant. |
ANS( Compute("k")->cmp() ); |
And in the answer and solution section of the file we can refer to the constant in the solution to the problem. |