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

Gerd Kortemeyer korte at lite.msu.edu
Mon Sep 2 16:19:31 EDT 2013


Hi,

For starters, I would use a problem with only one part and three answer fields inside the customresponse. Evaluation of the input should be inside the <answer>-block:

<customresponse …>
<startouttext />…<endouttext …>
<textline …>
<startouttext />…<endouttext …>
<textline …>
<startouttext />…<endouttext …>
<textline …>
<answer …>
</answer/>
</customresponse>

The answers will be in an array $$submission[0], $$submission[1], …

If any of those are (still) zero, return "MISSING_ANSWER"

In the very last step, calculate and check what you want, return "EXACT_ANS" or "INCORRECT".

- Gerd.


On Sep 2, 2013, at 3: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



More information about the LON-CAPA-users mailing list