[LON-CAPA-users] Custom Response Division by Zero

Todd Ruskell todd.ruskell at gmail.com
Mon Sep 2 16:45:15 EDT 2013


Hi Josh,

The easy solution is to move this calculation to the <part> that requires
it.  You are allowed multiple <script> blocks inside a problem.  So you can
split up the single <script> block that occurs at the top of your problem
into several different <script> blocks belonging to each <part>.

Then, since you use "Show Parts One-at-a-Time" the <script> of each part
shouldn't be evaluated until the &submission() values from the previous
<part> have been entered.

Todd


On Mon, Sep 2, 2013 at 1:03 PM, Josh Wolosz <jwolosz at slrsd.org> wrote:

> Hi Everyone,
>
> I have a question in regards to Custom Response problems.  I would like to
> try moving some of my labs into lon-capa, and one of the first ones
> involves working with percent error (for high school freshman).
>
> Here's what I would like to have happen:
>
> 1) Students enter an estimated value
> 2) Students enter the actual/measured value
> 3) Students then input their percent error which loncapa calculates and
> determines if they are correct or not
>
> Here is the script I have tried:
>
> *<script type="loncapa/perl">
> # Get the entered answer. First argument is partID, second is responseID
> $given=&submission(1,11);
> $example='You chose '.$given.' m';
> $given2=&submission(3,4);
> $example2='You chose '.$given2.' m';
> $a=&abs($given2-$given)/$given2;
> </script>*
>
> I'm getting an error returned saying there is an illegal division by zero,
> specifically:
>
> *$a=&abs($given2-$given)/$given2;
>
> *
> I understand that at the beginning of the problem nothing has been
> submitted, so $given2 has no value.  Is there a way around this?  Thanks
> for your help, and I apologize if it is a very simple solution that I am
> missing.
>
> Sincerely
> Joshua Wolosz
>
> The full code is:
>
> <problem>
>
>     <parameter name="ordered" id="13" type="string_yesno" default="no"
> description="Show Parts One-at-a-Time" />
>
> <script type="loncapa/perl">
> # Get the entered answer. First argument is partID, second is responseID
> $given=&submission(1,11);
> $example='You chose '.$given.' m';
> $given2=&submission(3,4);
> $example2='You chose '.$given2.' m';
> $a1=100*&abs($given2-$given)/$given2;
> </script>
>
> <part id="1">
> <startouttext />
> <i><b>Note:</b> You do not need to include the units when you input your
> calculations.</i><br></br>
>
> What did you estimate the width of the object to be?
> <endouttext />
> <customresponse answerdisplay="$example" id="11">
>     <answer type="loncapa/perl"># We do not want a vector
> if ($submission=~/\,/) { return 'EXTRA_ANSWER'; }
> # No units needed
> if ($submission=~/^\d+\s+\w+$/) { return 'UNIT_NOTNEEDED'; }
> # Need a numerical answer here
> if ($submission!~/^[\d\.]+$/) { return 'WANTED_NUMERIC'; }
> { return 'EXACT_ANS'; }</answer>
>     <textline readonly="no" />
> </customresponse><startouttext />meters<endouttext />
> </part>
>
> <part id="3">
> <startouttext />
> What did you <u>measure</u> the width of the object to be?
> <endouttext />
> <customresponse answerdisplay="$example2" id="4">
>     <answer type="loncapa/perl"># We do not want a vector
> if ($submission=~/\,/) { return 'EXTRA_ANSWER'; }
> # No units needed
> if ($submission=~/^\d+\s+\w+$/) { return 'UNIT_NOTNEEDED'; }
> # Need a numerical answer here
> if ($submission!~/^[\d\.]+$/) { return 'WANTED_NUMERIC'; }
> { return 'EXACT_ANS'; }</answer>
>     <textline readonly="no" />
> </customresponse><startouttext />meters<endouttext />
> </part>
>
> <part id="5">
> <startouttext />
> What was your percent error for the width?
> <endouttext />
> <numericalresponse answer="$a1" id="194">
> <responseparam name="tol" type="tolerance" default="0.5%"
> description="Numerical Tolerance" />
>     <textline readonly="no" />
> </numericalresponse>
> </part>
> </problem
>
> ------------------------------
>
> When writing or responding, please remember that the Secretary of the
> Commonwealth of Massachusetts has determined that email is a public record.
>
> This communication may contain privileged or other confidential
> information. If you are not the intended recipient, or believe that you
> have received this communication in error, please do not print, copy,
> retransmit, disseminate, or otherwise use the information. Also, please
> indicate to the sender that you have received this email in error, and
> delete the copy you received.  Thank you for your cooperation.
>
>
> _______________________________________________
> LON-CAPA-users mailing list
> LON-CAPA-users at mail.lon-capa.org
> http://mail.lon-capa.org/mailman/listinfo/lon-capa-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.lon-capa.org/pipermail/lon-capa-users/attachments/20130902/3e81215e/attachment.html>


More information about the LON-CAPA-users mailing list