from otree.api import *
import random
doc = """
Your app description
"""
class C(BaseConstants):
NAME_IN_URL = 'GeneralInstructions'
PLAYERS_PER_GROUP = None
NUM_ROUNDS = 1
Init_pay = cu(8)
r_min = 0
N_size = 10 # this is the sample size
P_yellow = [0.75, 0.25] # yellow and green box respectively
P_Yellow_Box = 0.90
P_Yellow_Other = 0.20
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
rand_box = models.FloatField(initial=0.75)
round_yellow_n = models.IntegerField(initial=5)
round_green_n = models.IntegerField(initial=5)
report = models.IntegerField(min=0,
max=100)
report_other = models.IntegerField(min=0,
max=100)
round_payoff = models.IntegerField(initial=0)
payoff_final = models.CurrencyField(initial=0)
n_balls = models.IntegerField(
label='How many balls does each box have?',
min=0, max=10)
p1_g_Y = models.IntegerField(
label='What is the probability that one green marble is drawn '
'if the box is Yellow? '
'Answer in percentage points.',
min=0, max=100)
seq_length = models.IntegerField(
label='How many marbles will be drawn from the box every round?',
min=0, max=20)
hidden_box = models.BooleanField(
label='Is the box color revealed after the computer selects it?',
choices=[
[False, 'No'],
[True, 'Yes'],
]
)
indep_round = models.BooleanField(
label='Does the box selected or sequence drawn in a round affect the next ones?',
choices=[
[False, 'No'],
[True, 'Yes'],
]
)
same_seq = models.BooleanField(
label='Will you and Participant 2 observe a different sequence every round?',
choices=[
[False, 'No'],
[True, 'Yes'],
]
)
# FUNCTIONS
def n_balls_error_message(player, value):
print('value is', value)
if value < 4:
return 'There are more balls in the box.'
if value > 4:
return 'There are less balls in the box.'
def p1_g_Y_error_message(player, value):
print('value is', value)
if value < 25:
return 'The probability is larger.'
if value > 25:
return 'The probability is smaller.'
def seq_length_error_message(player, value):
print('value is', value)
if value < 10:
return 'There will be more balls drawn.'
if value > 10:
return 'There will be less balls drawn.'
def hidden_box_error_message(player, value):
print('value is', value)
if value != False:
return 'The box is secretly chosen.'
def indep_round_error_message(player, value):
print('value is', value)
if value != False:
return 'Each round is independent. ' \
'Another box and sequence are realized every round.'
def same_seq_error_message(player, value):
print('value is', value)
if value != False:
return 'The same sequence is observed by both every round.'
# PAGES
class General(Page):
pass
class BoxesMarblesSequences(Page):
form_model = 'player'
@staticmethod
def before_next_page(player: Player, timeout_happened):
player.rand_box = random.choices(C.P_yellow, k=1, weights=[C.P_Yellow_Box, 1 - C.P_Yellow_Box])[0]
marble_sample = random.choices([0, 1], k=C.N_size, weights=[1 - player.rand_box, player.rand_box])
yellow_n = sum(marble_sample)
player.round_yellow_n = yellow_n
player.round_green_n = C.N_size - yellow_n
class Sequences(Page):
form_model = 'player'
@staticmethod
def js_vars(player):
# this function generates the sequence of marbles to be pass to JS
rand_seq = [1] * player.round_yellow_n + [0] * player.round_green_n
random.shuffle(rand_seq)
return dict(
marble_sample=rand_seq,
N=C.N_size
)
class TestGral(Page):
form_model = 'player'
form_fields = ['n_balls',
'p1_g_Y',
'seq_length',
'hidden_box',
'indep_round',
'same_seq'
]
page_sequence = [General,
BoxesMarblesSequences,
Sequences,
TestGral]