from otree.api import * import math import pandas as pd import itertools import numpy as np import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'task2' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 RISK_TEMPLATE = "risk_assessment/templates/risk.html" LOW_VALUES = [28, 24, 20, 16, 12, 2] HIGH_VALUES = [28, 36, 44, 52, 60, 70] GAMBLE_CHOICES = [(i, f'Gamble {i + 1}') for i in range(6)] class Subsession(BaseSubsession): roll = models.IntegerField() def creating_session(subsession): roll = random.randint(0, 1) # zero for high roll and one for low roll #print(roll) subsession.roll = int(roll) class Group(BaseGroup): pass class Player(BasePlayer): gamble_choice = models.IntegerField(initial = -1) payoff_task2 = models.FloatField(initial = 0) def set_payoffs(self): if self.subsession.roll == 0: self.payoff_task2 = C.LOW_VALUES[self.gamble_choice] self.participant.event_task_2 = "A" else: self.payoff_task2 = C.HIGH_VALUES[self.gamble_choice] self.participant.event_task_2 = "B" self.participant.chosen_task_2 = self.gamble_choice + 1 self.participant.payoff_task2 = np.round(self.payoff_task2,2) # PAGES def make_gamble(gamble): html = "" html += f"{gamble[1]}" html += "A" html += f"{C.LOW_VALUES[gamble[0]]}" html += "50%" html += f"" html += "" html += "" html += "B" html += f"{C.HIGH_VALUES[gamble[0]]}" html += "50%" html += "" html += "" return html class risk_assessment(Page): form_model = 'player' #form_fields = ['gamble_choice'] @staticmethod def vars_for_template(player: Player): table_body = "" for i in C.GAMBLE_CHOICES: table_body += make_gamble(i) return dict(table_body=table_body) @staticmethod def live_method(player, data): player.gamble_choice = int(data) @staticmethod def error_message(player, values): if player.gamble_choice == -1: return "You must choose one of the six gambles" @staticmethod def before_next_page(player, timeout_happened): player.set_payoffs() page_sequence = [risk_assessment]