Page that won't let you continue until all questions are answered correctly #247
-
Dear community, I would like to implement a manipulation check in the form that participants have to answer all questions correctly efore continuing to the next page. Attached find a minimal experiment that should function as a basic framework to build on. I know how to "test" and count how many questions are answered correctly (see lower part of my code). I don't know however how to translate this into a way of letting participants continue or repeat the page. Thank you in advance! import alfred3 as al
exp = al.Experiment()
ManipulationCheck_list_MC = [
{"name": "MC_1_lo", "toplab": "This is true"},
{"name": "MC_2_hi", "toplab": "This is false"}
]
@exp.member
class lo_ManipulationCheck(al.Page):
title = "Manipulation Check"
def on_exp_access(self):
for i in ManipulationCheck_list_MC:
# The following two lines will skip the remaining iterations when the
# item list runs out of items, giving the code way more flexibility.
item = i
self += al.SingleChoiceButtons(
"True",
"False",
toplab=item['toplab'],
name=item['name'],
width="wide",
button_width="140px",
force_input=True,
font_size="normal",
position="center"
)
def on_first_hide(self):
attention_score = 0
if self.exp.values.get(
"MC_1_lo") == 1:
attention_score += 1
if self.exp.values.get(
"MC_2_hi") == 2:
attention_score += 1
if __name__ == "__main__":
exp.run() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi @slussinator , I am quite certain that this is possible. To go on, would you mind reducing your example to just two items and remove elements from the code that are not relevant to the question? I think this applies to the
Leads to: import alfred3 as al |
Beta Was this translation helpful? Give feedback.
-
There are two ways to do this. Because Option 2 is not documented, I recommend Option 1. Option 1: Page.custom_move()You can use the import alfred3 as al
exp = al.Experiment()
@exp.member
class Demo(al.Page):
def on_exp_access(self):
self += al.SingleChoiceButtons("True", "False", toplab="true" ,name="item1")
self += al.SingleChoiceButtons("True", "False", toplab="false", name="item2")
def custom_move(self):
attention_score = 0
if self.exp.values.get("item1") == 1:
attention_score += 1
if self.exp.values.get("item2") == 2:
attention_score += 1
if attention_score < 2:
self.exp.post_message("Test failed", level="danger")
return False
else:
return True
if __name__ == "__main__":
exp.run() Option 2: AbortMoveAlfred defines the exception import alfred3 as al
from alfred3.exceptions import AbortMove
exp = al.Experiment()
@exp.member
class Demo(al.Page):
def on_exp_access(self):
self += al.SingleChoiceButtons("True", "False", toplab="true" ,name="item1")
self += al.SingleChoiceButtons("True", "False", toplab="false", name="item2")
def on_first_hide(self):
attention_score = 0
if self.exp.values.get("item1") == 1:
attention_score += 1
if self.exp.values.get("item2") == 2:
attention_score += 1
if attention_score < 2:
self.exp.post_message("Test failed", level="danger")
raise AbortMove
if __name__ == "__main__":
exp.run() Because Option 2 is not documented, I recommend Option 1. |
Beta Was this translation helpful? Give feedback.
There are two ways to do this.
Because Option 2 is not documented, I recommend Option 1.
Option 1: Page.custom_move()
You can use the
Page.custom_move()
hook. This hook is used to conduct manually specified movements. Usually, you would useExperimentSession.forward()
orExperimentSession.backward()
orExperimentSession.jump()
inside this method. But if it returns a "falsy" value likeFalse
orNone
without any custom movement, alfred will just stay on the same page. To fall back to alfreds normal movement system, you can define a case in which the method returnsTrue
. In your example, that could look like this: