import random from otree.api import * author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'attention_check_test' players_per_group = None num_attention_check_tries = 2 num_game_rounds = 3 num_rounds = num_attention_check_tries + num_game_rounds - 1 payment_per_correct_answer = 1 class Player(BasePlayer): attention_check_rounds = models.IntegerField(label="How many rounds does the experiment have?") attention_check_payment = models.FloatField(label="How much are you paid per correct answer?") number_entered = models.IntegerField(label="Please enter the result.") correct_result = models.IntegerField() class Subsession(BaseSubsession): pass class Group(BaseGroup): pass # FUNCTIONS # PAGES class Instructions(Page): @staticmethod def is_displayed(player: Player): if player.round_number == 1: return True else: return player.participant.vars["failed_attention_check"] class AttentionCheck(Page): form_model = "player" form_fields = ["attention_check_rounds", "attention_check_payment"] @staticmethod def is_displayed(player: Player): if player.round_number == 1: return True else: return player.participant.vars["failed_attention_check"] @staticmethod def before_next_page(player: Player, timeout_happened): if ( player.attention_check_rounds == Constants.num_game_rounds and player.attention_check_payment == Constants.payment_per_correct_answer ): player.participant.vars["failed_attention_check"] = False else: player.participant.vars["failed_attention_check"] = True class FailedAttentionCheck(Page): @staticmethod def is_displayed(player: Player): return player.participant.vars["failed_attention_check"] class AddNumbers(Page): form_model = "player" form_fields = ["number_entered"] @staticmethod def is_displayed(player: Player): return player.round_number >= Constants.num_attention_check_tries @staticmethod def vars_for_template(player: Player): number_1 = random.randint(0, 100) number_2 = random.randint(0, 100) player.correct_result = number_1 + number_2 return { "number_1": number_1, "number_2": number_2, } @staticmethod def before_next_page(player: Player, timeout_happened): if player.number_entered == player.correct_result: player.payoff = Constants.payment_per_correct_answer class Results(Page): @staticmethod def is_displayed(player: Player): return player.round_number >= Constants.num_attention_check_tries page_sequence = [Instructions, AttentionCheck, FailedAttentionCheck, AddNumbers, Results]