WeBWorK Problems

MultiAnswer subroutine with strings

Re: MultiAnswer subroutine with strings

by Danny Glin -
Number of replies: 0

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).