WeBWorK Problems

contextReaction and complex compounds

contextReaction and complex compounds

by Eric Stroyan -
Number of replies: 3
contextReaction.pl works very nicely...but I have discovered a bit of a problem when entering compounds with complex ions.
Compounds such as Zn(ClO_{3})_{2} are not liked by the macro. Any suggestions? I have included a short sample problem below the error message generated by fixing the choice to a reaction scheme that triggers the error.
It may also be necessary for contextReaction to have a comma put in line 132 between Pb and Bi. I was having lead containing formulae rejected until I changed my copy.
Thanks for all the help.

Error messages

The left-hand side of '_' must be an element, not a compound; see position 17 of formula at line 395 of [TMPL]/macros/contextReaction.pl Died within context::Reaction::BOP::underscore::_check called at line 35 of [PG]/lib/Parser/BOP.pm from within Parser::BOP::new called at line 494 of [PG]/lib/Parser.pm from within Parser::Precedence called at line 381 of [PG]/lib/Parser.pm from within Parser::Close called at line 145 of [PG]/lib/Parser.pm from within Parser::parse called at line 50 of [PG]/lib/Parser.pm from within Parser::new called at line 19 of [PG]/lib/Value/Formula.pm from within Value::Formula::new called at line 64 of [PG]/macros/Parser.pl from within main::Formula called at line 105 of [TMPL]/chem1l2/predicting_products/pred_prod.pg

The file responsible
# Predict products of a chemical reaction.
# Student presented with the (balanced) reactants and asked for products.
# Answer must present balanced products.
# If a reaction does not proceed as written the product is NR.
# Normally this hash would be stored under macros and called above
1=>{equation=>"N_{2} + H_{2} \longrightarrow NH_{3}",
{type=>"reactant", compound=>"N_{2}", coef=>1 },
{type=>"reactant", compound=>"H_{2}", coef=>3 },
{type=>"product", compound=>"NH_{3}", coef=>2 },]},
2=>{equation=>"Cu + Zn(ClO_{3})_{2} \longrightarrow NR",
{type=>"reactant", compound=>"Cu", coef=>"" },
{type=>"reactant", compound=>"Pb(ClO_{3})_{2}", coef=>"" },
{type=>"product", compound=>"NR", coef=>"" },]},
3=>{equation=>"CuNO_{3} + CuCl \longrightarrow NR",
{type=>"reactant", compound=>"CuNO_{3}", coef=>"" },
{type=>"reactant", compound=>"CuCl", coef=>"" },
{type=>"product", compound=>"NR", coef=>"" },]},
Context()->variables->add(NR => $context::Reaction::CONSTANT);
# Pick a reaction from the selected database
$number_of_reactions = keys %reactions;
$S = list_random(@items);
# Get skeleton equation, type of reaction, then get
# info about coefficients and calculate formula masses
$equation = $reactions{$S}{equation};
$type = $reactions{$S}{class};
for $who (@{$reactions{$S}{components}})
if($kind eq "reactant")
{$n_r =$i;
$react[$i] = $who->{compound};
if($kind eq "product")
$prod[$j] = $who->{compound};
# Build reactants and products sides of equation
$reactants = '';
for ($i=0;$i<=$n_r;$i++)
{ if($i < $n_r)
{$and = ' + ';}
if($i ==$n_r)
{$and = '';}
$r_c = '';
if($react_coef[$i] != 1)
{$r_c = $react_coef[$i]}
$reactants = $reactants.$r_c.$react[$i].$and;
$products = '';
for ($i=0;$i<=$n_p;$i++)
{ if($i < $n_p)
{$and = ' + ';}
if($i ==$n_p)
{$and = '';}
$p_c = '';
if($prod_coef[$i] != 1)
{$p_c = $prod_coef[$i]}
$products = $products.$p_c.$prod[$i].$and;
$react = Formula($reactants);
$prod = Formula($products);
For the following problem your answer must be entered in LaTeX format.
as discussed in lecture.
Predict the products for the $type reaction shown.$BR
If a combination does not react the products should be input as NR.$BR
\($react \longrightarrow\) \{ans_rule(30)\}.
In reply to Eric Stroyan

Re: contextReaction and complex compounds

by Davide Cervone -

I have updated the contextReaction.pl file to allow subscripts on compounds as well as elements. (But not on molecules; should that be allowed, too? I.e., do you want (O_2)_3?)

I'm not currently set up to update the SVN repository, so I'm attaching the updated file here. I corrected the problem with Pb as well. Thanks for the catch.

Mike, perhaps you can update the SVN version? Thanks.