I've written the following code to create a multiple choice button that takes an arbitrary number of answer options and will assign a variable true/false depending on whether or not the correct choice is selected:
mchoice[question_, answers_, correct_, var_] :=
DynamicModule[{x},
Column[{Dynamic[var = (x == correct);], (*problem line *)
Dynamic@
ActionMenu[question,
MapThread[#1 :> (x = #2) &, {answers, Range@Length@answers}]],
PaneSelector[
MapThread[#1 -> #2 &, {Range@Length@answers, answers}],
Dynamic[x]]
}]
]
The code is executed as below and gives the button:
Clear[avar]
mchoice["argon", {"solid", "liquid", "gas"}, 3, avar]
The correctness of the selection is stored in avar
which I can evaluate at the end of the notebook to "grade" the quiz, if you will. The problem is the presence of Null
above the button. With the code as written, I cannot seem to create a button that both updates the value of avar and doesn't print the answer. For example, if I substitute Dynamic[var = (x == correct)]
for the problem line, then the student will know if the chosen answer is correct. If I use Dynamic[var = (x == correct)];
then the result is visually correct but avar
will not update when the student selects an answer. I can get the desired result by using Dynamic[var = (x == correct);] /. {Null ->""}
but that feels like I'm cheating (yes, I'm getting the desired result, but no, I'm not using Dynamic
correctly).
I'm interested in either of the following:
- A better solution to my (* problem line *)
- Alternative methods for creating a multiple choice button.
RadioButtonBar
, it would certainly make things simpler for you as well, code-wise. $\endgroup$Dynamic
beforeActionMenu
is useless ? $\endgroup$