PREP 2014 Question Authoring - Archived

Customizing error messages

Re: Customizing error messages

by Davide Cervone -
Number of replies: 0
OK, I've done a similar arrangement for the decimal notation. Getting it to be used by default is a little more complicated, and I'm not sure this will work for every problem in the OPL, but it should cover quite a few.

The documentation from the file is included below. It will look familiar.

One thing to note is the section on lists. I added a semi-colon as an alliterative to commas, since there is ambiguity using commas for decimals and lists. I'm not sure how you usually handle something like that using comma notation.

Also, note that I'm forcing comma-users to write 0,3 and 3,0 rather than ,3 or 3, in order to facilitate the distinction between numbers and lists. Let me know if that doesn't work for you.


NAME

Context("AlternateDecimal") - Provides a context that allows the entry of decimal numbers using a comma for the decimal indicator rather than a dot (e.g., 3,14159 rather than 3.14159).

DESCRIPTION

This macro file defines contexts in which decimal numbers can be entered using a comma rather than a period as the decimal separator. Both forms are always recognized, but you can determine whether one or the other form produces an error message when used. You can also force the display of numbers to use one or the other form.

USAGE

To use this file, first load it into your problem, then select the context that you wish to use. There are three pre-defined contexts, AlternateDecimal, AlternateDecimal-Only, and AlternateDecimal-Warning. The first allows both the standard and alternate forms to be used, the second allows only the alternate form, and the third allows only the standard form, but recognizes the alternate form and gives an error message when it is used.
	loadMacros("contextAlternateDecimal.pl");
	
	Context("AlternateDecimal");
	
	$r1 = Compute("3.14159");
        $r2 = Compute("3,14159");    # equivalent to $r1;
	
	Context("AlternateDecimal-Only");
	
	$r1 = Compute("3.14159");
        $r2 = Compute("3,14159");    # causes an error message
	
	Context("AlternateDecimal-Warning");
	
	$I1 = Compute("3.14159");    # causes an error message
        $I2 = Compute("3,14159");
There are two context flags that control the input and output of decimals.

enterDecimals => "either" (or "," or ".")

This specifies what formats the student is allowed to use to enter a Decimal. A value of "either" allows either of the formats to be accepted, while the other two options produce error messages if the wrong form is used.

displayDecimals = "either" (or "," or ".")

This controls how decimals are displayed. When set to "either", the decimal is displayed in whatever format was used to create it. When set to "." or ",", the display is forced to be in the given format regardless of how it was entered.

The AlternateDecimal context has both flags set to "either", so the decimals remain in the format the student entered them, and either form can be used. The AlternateDecimal-Only context has both set to ",", so only the alternate format can be used, and any number will be displayed in the alternate format. The AlternateDecimal-Warning context has both set to ".", so only standard format can be used, and all numbers will be displayed in standard format.

It is possible to set enterDecimals and displayDecimals to different values. For example.

	Context()->flags->set(
	  enterDecimals => "either",
	  displayDecimals => ".",
	);
would allow students to enter decimals in either format, but all numebrs would be displayed in standard form.

LISTS IN ALTERNATE FORMAT

Because the alternate format allows numbers to be entered using commas rather than periods, this makes the formation of lists harder. For example, 3,5 is the number 3-and-5-tenths, not the list consisting of 3 followed by 5. Because of this ambiguity, the AlternateDecimal contexts also include the semi-colon as a replacement for the comma as a separator. So 3;5 is the list consisting of 3 followed by 5, and 3,1;5.2 is the list consisting of 3.1 and 5.2.

Note that the comma is still available for use as a separator, but this makes things like 3,2,1 tricky, because it is not clear if this is 3.2 followed by 1, or 3.2 times .1, or the list of 3, 2, and 1. To help make this unambiguous, numbers that use a comma as decimal inidcator must have a digit on both sides of the comma. So one tenth would have to be entered as 0,1 not just ,1 (but you can still enter .1. Similarly, You must enter 3,0 or just 3 rather than 3,, even though 3. is acceptable.

With this notation 3,2,1 means the list consisting of 3.2 followed by 1. If you want the list consisting of 3 followed by 2.1, you could use 3, 2,1 since the comma in 3, is not part of the number, so must be a list separator.

SETTING THE ALTERNATE FORM AS THE DEFAULT

If you want to force existing problems to allow (or force, or warn about) the alternate format instead, then create a file named parserCustomization.pl in your course's templates/macros directory, and enter the following in it:
	loadMacros("contextAlternateDecimal.pl");
        context::AlternateDecimal->Default("either","either");
        Context("Numeric");
This will alter all the standard contexts to allow students to enter numbers in either format, and will display them using the form that was used to enter them.

You could also do

	loadMacros("contextAlternateDecimal.pl");
        context::AlternateDecimal->Default(".",".");
        Context("Numeric");
to cause a warning message to appear when students enter the alternate format.

If you want to force students to enter the alternate format, use

	loadMacros("contextAlternateDecimal.pl");
        context::AlternateDecimal->Default(",",".");
        Context("Numeric");
This will force the display of all numbers into the alternate form (so even the ones created in the problem using standard form will show using commas), and will force students to enter their results using commas.