WeBWorK Problems

MultiAnswer subroutine with strings

MultiAnswer subroutine with strings

by David Nawrocki -
Number of replies: 1

I'm trying to construct a problem that accepts multiple string answers like "p=0.12" or "p=.12" as correct answers.  I tried using the MultiAnswer subroutine but it does not seem to accept strings as input variables.  So, something like 

$fac1 = 'p=0.12';
$fac2 = 'p=.12';
$multians = MultiAnswer($fac1, $fac2)->with(
  singleResult => 0,
will result in an error message.  If $fac1 and $fac2 are constants or formulas it works fine but trying to pass strings gives me an error.  Any suggestions would be appreciated.  
In reply to David Nawrocki

Re: MultiAnswer subroutine with strings

by Danny Glin -

I haven't testing what follows, but it should work.

When passing variables to MultiAnswer() it is safest to make sure that they are already MathObjects.  In your case you are passing raw perl strings.  When that happens I believe that MultiAnswer calls Compute() on each of those variables.  Compute() then parses the string and tries to convert it to an appropriate MathObject in the current context.  You didn't post the full problem code, so I'm assuming that this is in the Numeric context (because this is the default, though you may have set it explicitly using Context('Numeric'); ).  In that context Compute() will be able to parse real numbers and formulas (as you discovered), but doesn't know what to do with anything else.

You should be able to resolve this by explicitly creating string MathObjects for your two strings:

$fac1 = String('p=0.12');
$fac2 = String('p=.12');

Having said all that, I'm not sure that MultiAnswer is what you're looking for here.  MultiAnswer is intended for questions where there are multiple answer blanks that depend on one another.  If you're dealing with a single answer blank with multiple correct answers, then there are a couple of ways to do this:

  • Use parserOneOf.pl - though this could be confusing when students view the correct answer, as it will show all of the options that you have provided.
  • Write a custom answer checker that checks for each correct answer.

If your answer is specifically of the form 'p=[a number]', then you can use parserAssignment.pl, which will force students to enter the "p=" portion, but will still take advantage of WeBWorK's numerical checking (e.g. it will accept "p=0.12", "p=.12", "p=3/25", and you can even set numerical tolerances).