Formules: Variables

De MoodleDocs
Aller à :navigation, rechercher

Remarque : la traduction de cette page n'est pas terminée. N'hésitez pas à traduire tout ou partie de cette page ou à la compléter. Vous pouvez aussi utiliser la page de discussion pour vos recommandations et suggestions d'améliorations.


Les variables peuvent être utilisées pour substituer des textes de question, définir les réponses et spécifier des critères de notation. L'un des principaux objectifs du système de variables est de simplifier la tâche pour faire des variations de question. Puisque les variables sont générées au début d'un test, le test sera arrêté si des erreurs surviennent. Par conséquent, afin de minimiser les erreurs, le système est conçu pour n'avoir que des variables déterministes, des listes de longueur constante et aucun branchement.

Portée des variables

Il y a quatre types de variables (au hasard, globales, locales et de notation) que vous pouvez définir et manipuler dans les champs suivants :

Champ Portée d'application
Variables au hasard (ou aléatoires) L'endroit pour définir la variation des variables pour l'ensemble de la question.
Variables globales La portée inclut toutes les variables aléatoires (instanciées).
Variables locales La portée inclut toutes les variables globales. Remarque: Chaque sous-question a sa propre portée de variables locales.
Variables de notation La portée inclut toutes les variables locales, plus des variables spéciales (par exemple _0, _1) en fonction de la réponse des étudiants.

De plus, vous pouvez utiliser les quatre types de variables dans les champs suivants :

Champ Portée d'application
Texte de la question principale Toutes les variables globales peuvent être utilisées en substitution.
Texte de la partie Toutes les variables locales pour la partie peuvent être utilisées en substitution.
Réponse Toutes les variables locales pour la partie peuvent être utilisées dans l'expression.
Critère de notation Toutes les variables de notation peuvent être utilisées dans l'expression.

Names of variables

A variable name is a string of alphanumeric characters (a-z, A-Z, 0-9 and _) that cannot start with a number or the underscore. Examples of valid variable names are:

x, y1, z_1, foo_bar, myFirstVariable

Variable names are case-sensitive so that, for example, the following are four different variables:

foobar, FooBar, fooBar, FOOBAR

Types of variables

Each variable will be assigned implicitly with a type. A type is either number, string or algebraic variable, plus list of numbers or list of strings. List is defined as the elements enclosed by [ and ]. An element in list can be referred using the syntax A[b], for example [4,5,6][0] gives the first number, that is, 4. These types are listed below:

Type Description
Number A number, for example: 1.2e-3
String Characters enclosed by two double quotes, for example: "Hello"
List of number Numbers enclosed by [ and ] , for example: [4,5,6] , The equivalent short hand syntax are [4:7] or [4:7:1.]
List of string Strings enclosed by [ and ] , for example: ["A", "B", "C"]
Algebraic variable It is simply a set of numbers (see syntax) defined in the non-random variable scope, for example: {1:100}

Random variables

During the quiz creation, each random variable will be assigned by ONE value defined by the expression. Hence, each student can have their own set of values for the quiz attempt.

A random variable has different syntax than other variables and it is only defined in the field Random variables. These variables can be defined by assigning a set of elements or a shuffling of a list. The probability of selecting each element is equal so that each element has equal chance to be drawn. There are three main types of expressions:

Set of elements

A set of elements is elements enclosed by { and }. The element can be either a number, a string, a number list or a string list. For example, the variable below is a set of list of numbers, and the probability for element [2,3] is 1/5:

F = {[0,0], [1,1], [2,4], [3,9], [4,16]};

Set of numbers

A set of numbers is numbers enclosed by { and }. You can also specify a range in the format of {start:stop:interval}, where the numbers satisfying (start + n*interval) < stop, n = 0,1,2,3,..., will be generated. If the interval is not specified, value 1 will be used. Note that the stop value is not included by definition, but may be included for non-integers due to numerical errors. The set of numbers defined below demonstrate different range syntaxes. The probabilities of the elements in each set are 1/8, 1/10 and 1/20 respectively.

A = {1,2,3,4,5,6,7,8};

B = {0:1:0.1};

C = {1:10, 10:100:10, 100, 200};

Shuffled list

A list can be passed to the shuffle function. In the example below, the instantiated variable S with take one of the permutation of the input list, say [4,3,2,5] or [2,5,4,2] The probability of each list is therefore 1/4! = 1/24.

S = shuffle([2,3,4,5]);

Variable assignments

Variable assignments allow you to define and manipulate variables. It can be defined in the option field of global variables, local variables and grading variables.

Component Definition
Expression Any combination of numbers and variables together with operators and functions listed in the Appendix. Typically, it is just a simple mathematical formula evaluated to a number. The variables used in the expression must be defined before.
Assignment Used to assign the evaluated result of an expression to a variable, in the form of 'name = expression ;'
For loop Allows a simple iteration in the form of 'for(element:list)'. All elements in the list are iterated.

Examples of assignments are shown below:

Example Comments
a = 1; # text after "#" and the end of line are treated as comment
b = exp(3.333);
d = round(b, 1); # the function round() can round the number to the desired decimal
e = 1 + sin(2) + pow(a,2);
A = [1,2,3];
x = A[0];
y = A[a];
z = A[2];
w = A[0] + A[1] + A[2];
m = max(x, y);
distance = sqrt(x*x + y*y + z*z);
theta = atan2(y, x);
smaller = x < y; # smaller is 1
con = (x < y) + (y < z); # con is 2
B = fill(3,0); # B is now [0,0,0]
C = map("sqrt",fill(3,16)); # square root of [16,16,16], so C is now [4,4,4]
s = 0;
for (i:A) s = s + i; # s is 6 after the loop
for (i:[0:3]) {

    B[i] = sum(map("+",A,fill(3,i)));

   }

# B will be [6,9,12]
p = pick(a+9,"","A","B"); # pick() always choose the first element if index out of range
u = {-3,-2,-1,1:100}; # u and v define algebraic variables, the numbers are the points for evaluation
v = {-100:100:1};

Notes:

  • Important! Index out of range cannot be checked by the validation during question save, so you have to check it yourself. Otherwise, the quiz initialization for some students may fail. Use pick() as a safe variant if applicable.
  • Logical true is treated as 1 and false is 0, so the variable con above gets the value 2.
  • There is no branching, however, you may use the ternary operator (condition) ? (true) : (false) for numbers or the pick() function for general case.
  • The available functions are listed in the Appendix. There are many additional functions for the numeric list operation, and a few for the list of strings.


Accéder au haut de la page

← QUESTION DE TYPE FORMULES

Autres langues

English
Español