WeBWorK Main Forum

Equation in the answer

Equation in the answer

by Adam Z -
Number of replies: 14

I have this part of my question: 

BEGIN_PGML

State sales tax [`y`] is directly proportional to retail price [`x`]. An item that sells for [$a] dollars has a sales tax of [$b] dollars. Find a mathematical model that gives the amount of sales tax [`y`] in terms of the retail price [`x`] 

The equation is [`y`]= [_______________]{"$k*x"}

But I want students to write the answer y= number*x , I tried this, but did not work : 

The equation is [_______________]{"y=$k*x"}

Advice, please.

In reply to Adam Z

Re: Equation in the answer

by Andras Balogh -

Probably this is what you are looking for: https://webwork.maa.org/wiki/EquationEvaluators

Note that this will also accept all equivalent equations to y=$k*x, for example

y/$k=x  or (y+10)/$k=x+10/$k. 


In reply to Andras Balogh

Re: Equation in the answer

by Alex Jordan -

If you would like to enforce that the form of the answer be `y=...` then you should look into parserAssignment.pl (https://webwork.maa.org/pod/pg/macros/parserAssignment.html). These objects must be in the form "variable = expression". You are assigning a formula to the variable on the left.

In reply to Alex Jordan

Re: Equation in the answer

by Adam Z -

Thank you so much, Alex. Although I do not understand these packages, it worked :) thank you again. But I had another issue with Currency. Can not I use more than one Context ? since after adding 

Context("Numeric")->variables->add(y=>'Real');

        parser::Assignment->Allow;

I go an error from the " Currency" context.  it is in bold . 

DOCUMENT();        # This should be the first executable line in the problem.

loadMacros(

   "PGstandard.pl",     # Standard macros for PG language

   "MathObjects.pl",

   "PGML.pl",

   "contextCurrency.pl",

   "PGcourse.pl",

   "parserImplicitEquation.pl",

    "parserAssignment.pl",

);

TEXT(beginproblem());

Context("Currency");

$showPartialCorrectAnswers = 1;

Context("Numeric")->variables->add(y=>'Real');

        parser::Assignment->Allow;

$n=random(1,10,1);

$b=random(1,20,1); 

$c=random(50,500,1);

$a=$n*$b;

$k=$a/$b;

$d=Currency($k*$c);

$f = Formula("y=$k*x");

BEGIN_PGML

State sales tax [`y`] is directly proportional to retail price [`x`]. An item that sells for [$a] dollars has a sales tax of [$b] dollars. Find a mathematical model that gives the amount of sales tax [`y`] in terms of the retail price [`x`]

The equation is [_______________]{"$f"}

What is the sales tax on a [$c] dollars purchase?

The answer is [_______________]{$d}

END_PGML

ENDDOCUMENT();       # This should be the last executable line in the problem.



In reply to Adam Z

Re: Equation in the answer

by Andrew Parker -

You may use multiple Contexts within the same problem, but the issue here is that the Context("Numeric") continues to apply until you declare a new Context. You will need to re-arrange your code so that you change the context to "Currency" (from "Numeric") before declaring your Currency variable.

TEXT(beginproblem());

$showPartialCorrectAnswers = 1;

Context("Numeric")->variables->add(y=>'Real');

        parser::Assignment->Allow;

$n=random(1,10,1);

$b=random(1,20,1); 

$c=random(50,500,1);

$a=$n*$b;

$k=$a/$b;

$f = Formula("y=$k*x");

Context("Currency");

$d=Currency($k*$c);


Also, because $f is now declared as a MathObject (Formula), you can use it in PGML as the answer directly (without enclosing quotes). With the quotes, the answer object will be re-created from the string "y = $k*x", and the most recent context was "Currency", so it will be recreated in the new context (where parser::Assignment->allow has not been set, and y has not been set as a context variable) and likely cause an error.

The equation is [_______________]{$f}



In reply to Adam Z

Re: Equation in the answer

by Alex Jordan -
Andrew has you covered for that issue. I thought I'd mention a few unrelated things:
* with more recent versions of WeBWorK, it's better it you leave out the line with TEXT(beginproblem());.
* for PGML answer blanks, you might like using [_]{$f}{12} instead of [_______________]{$f}.
In reply to Alex Jordan

Re: Equation in the answer

by Adam Z -
Thank you, Alex. The second point is clear. Where do I need to place TEXT(beginproblem()); ?
In reply to Adam Z

Re: Equation in the answer

by Alex Jordan -

> Where do I need to place TEXT(beginproblem()); ?

What I mean is, you do not need that line at all. It was in your sample code earlier and you can delete it.

In reply to Alex Jordan

Re: Equation in the answer

by Adam Z -

Ohh, yes I understand now. Thank you again, Alex. I do have another off-topic question. How to find an " organized, structured, comprehensive tutorial of this PERL syntax 

In reply to Adam Z

Re: Equation in the answer

by Andrew Parker -

Personally, I like "Perl in 2 hours 30 minutes"

https://qntm.org/perl_en

Note that this is restricted to solely Perl syntax, and PG (the WeBWorK problem coding language) is built on top of Perl. You'll also want to read through the details of MathObjects (the OOPerl classes):

https://webwork.maa.org/wiki/Introduction_to_MathObjects

https://webwork.maa.org/wiki/Introduction_to_Contexts

https://webwork.maa.org/wiki/Category:MathObject_Classes

https://webwork.maa.org/wiki/Common_MathObject_Methods