WeBWorK Main Forum

Easiest way to have string answers with PGML? Need some examples.

Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Number of replies: 8
I know how to add strings to a Context object with PGML problems but I wonder if there is an easier way.

I saw some docs that mentioned String() but I couldn't find examples.

I'm hoping with some context I don't know about that the following will work or a close variant.....

BEGIN_PGML
What is capital of Texas? [____________]{String("Austin")}
END_PGML
In reply to Christian Seberino

Re: Easiest way to have string answers with PGML? Need some examples.

by Paul Pearson -
Hi Christian,

I don't know about how to do this with PGML. There is a macro "contextArbitraryString.pl" and a context Context("ArbitraryString"); that may help you. In particular, it looks like this macro allows students to enter any string as an answer (so you don't have to add any strings to the context), but you will have to write a custom answer checker. The macro file is at

http://webwork.maa.org/viewvc/system/trunk/pg/macros/contextArbitraryString.pl?view=markup

Notice that there is a short example near the top of the macro file (in the part that is commented out by #'s). I'd also be interested to learn the answer to your original question (using PGML), so I hope that someone else has the answer.

Best regards,

Paul Pearson
In reply to Paul Pearson

Re: Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Thanks. That worked exactly as expected in PGML.

I didn't need the String function after using the correct context you suggested.

I'm still confused what String("somestring") is used for.

cs
In reply to Christian Seberino

Re: Easiest way to have string answers with PGML? Need some examples.

by Davide Cervone -
Can you post your final version of the problem, so others can see how you handled it?

As for String("something"), this is used to obtain an answer that is one of the pre-defined strings in your Context. For example, "NONE" or "DNE". You can add more strings to the Context, depending on what you want the student to be able to type. If the student types something that is not one of the predefined strings (and doesn't parse as a mathematical expression), the student will get a warning message indicating that.

There is a contextString.pl context that is useful if you have a list of possible answers that a student is allowed to type (like A, B, C, D, or POSITIVE, ZERO, NEGATIVE, or some such).

The contextArbitraryString.pl macro file defines a context in which a student can type anything without any error messages, leaving the responsibility to you to do all the parsing and error checking. For your problem, you will need to be careful to do things like strip leading and trailing spaces, and decide if you care about upper- and lower-case difference, and so forth.

In normal PG, if you want the answer be the word "NONE", you would use ANS(String("NONE")->cmp). In PGML, however, you don't really need to use String(), since using [______]{"NONE"} is sufficient.
In reply to Davide Cervone

Re: Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Here is what I did below. It probably would have been more elegant to use multiple choice format instead.


DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
"parserNumberWithUnits.pl",
"contextArbitraryString.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
######################################################################

Context("ArbitraryString");

BEGIN_PGML
What happens to the pressure if the number of atoms is increased while keeping
everything else the same?
(Answer either increases, decreases
or same.) [____________]{"increases"}
END_PGML

######################################################################
ENDDOCUMENT();

In reply to Christian Seberino

Re: Easiest way to have string answers with PGML? Need some examples.

by Paul Pearson -
Hi Christian,

I think Moodle just timed out and ate my last reply...so here is the second (abridged) version.

When there are only a limited number of choices, I recommend using a multiple choice list or pop-up menu (a.k.a. drop down menu) so that there is no room for student error. A multiple choice list will display all of the options to the student in HTML and PDF modes, while the pop-up list will only display the options in HTML mode and so you should probably include "(Answer either increases, decreases or stays the same.)" as part of the problem text for the benefit of students using a PDF copy.

If students are asked to enter a string, then some of them will enter a "correct" answer with the wrong syntax, such as "increses" instead of "increases". While there is some pedagogical value to having students find and correct such mistakes themselves, these sorts of difficulties usually frustrate students and cause them to dislike webwork (and to dislike you for choosing webwork). Also, it is time consuming for professors to read and reply to emails from students who swear they have the correct answer. In general, it's a good idea to do things like using multiple choice answer checkers that make webwork more student friendly.

When I write a question like this, I like to make it a two-part question. The first part will be multiple choice and the second part will be open ended (e.g., a "concrete" question with some numbers or variables). Then, I use the standard answer grader, which requires that all answers be correct before awarding credit, and choose whether to give students feedback on which parts are correct by setting $showPartialCorrectAnswers. See, e.g.,

http://webwork.maa.org/wiki/WeightedGrader

Good luck!

Paul Pearson
In reply to Paul Pearson

Re: Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Paul

Thanks for the reply. I agree a drop down menu or radio buttons are the way to go.

Do you have a PGML example or this?

Thanks!

cs
In reply to Christian Seberino

Re: Easiest way to have string answers with PGML? Need some examples.

by Davide Cervone -
Here is an example that uses a popup menu. You have to create the PopUp() before the BEGIN_PGML/END_PGML block in order for it to work.
    DOCUMENT();

    loadMacros(
      "PGstandard.pl",
      "PGML.pl",
      "MathObjects.pl",
      "parserPopUp.pl",
      "PGcourse.pl",
    );
 
    TEXT(beginproblem());
    
    $popup = PopUp(["increases","decreases","stays the same"],"increases");    
        
    BEGIN_PGML
    What happens to the pressure if the number of atoms is increased while keeping
    everything else the same? [____________]{$popup}
    END_PGML

    ENDDOCUMENT();
You can do something similar for radio buttons using parserRadioButtons.pl.

Davide

In reply to Christian Seberino

Re: Easiest way to have string answers with PGML? Need some examples.

by Davide Cervone -
There are a couple of issues with this approach. For example, if the student types " increases" with a space before it, it will be marked incorrect. Same for "INCREASES", and so on for a variety of other capitalization issues. The ArbitraryString context is really meant when you want to process the student response without any interference from the MathObjects parser, and it really expects you to provide your own custom checker to do that.

You might be better off with the String context (available in contextString.pl". For this, you define the strings you want to allow, and then the student will get an error message indicating the allowed strings if he types anything else. That should take care of Paul's concern about leaving off a letter, for example.

Here is one version:

    DOCUMENT();

    loadMacros(
      "PGstandard.pl",
      "PGML.pl",
      "MathObjects.pl",
      "contextString.pl",
      "PGcourse.pl",
    );
    
    TEXT(beginproblem());
    $showPartialCorrectAnswers = 1;
    
    Context("String");
    Context()->strings->are(
      increases => {},
      decreases => {},
      "stays the same" => {},
    );
    
    BEGIN_PGML
    What happens to the pressure if the number of atoms is increased while keeping
    everything else the same?
    (Answer either "increases", "decreases"
    or "stays the same".) [____________]{"increases"}
    END_PGML
    
    ENDDOCUMENT();
Of course, the pop-up or radio buttons approach is probably better, but if you want to uses typed strings, the String context gives students a hand with typing problems.

Davide