from otree.api import *
from settings import LANGUAGE_CODE
import time
import random
class C(BaseConstants):
NAME_IN_URL = 'task1'
PLAYERS_PER_GROUP = None
NUM_ROUNDS = 1
class Subsession(BaseSubsession):
test_median = models.FloatField(initial=0)
class Group(BaseGroup):
pass
class Player(BasePlayer):
test_score = models.IntegerField(initial=0)
time_start = models.FloatField(label='Time: Start')
time_end = models.FloatField(label='Time: End')
icar_q1 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, '
None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q2 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q3 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q4 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q5 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q6 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q7 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q8 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q9 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q10 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
icar_q11 = models.IntegerField(
choices=[[1,'
'],
[2,'
'],
[3,'
'],
[4,'
'],
[5,'
'],
[6,'
'],
[7, 'None of these
'],
[8, "I don't know
"]],
label="",
widget=widgets.RadioSelectHorizontal,
blank=True
)
# FUNCTIONS
def compute_score(player: Player):
# test score
##correct answers
answers = [4, 3, 5, 2, 2, 4, 5, 3, 1, 4, 5]
##get players answers
submission = [player.icar_q1, player.icar_q2, player.icar_q3, player.icar_q4, player.icar_q5, player.icar_q6,
player.icar_q7, player.icar_q8, player.icar_q9, player.icar_q10, player.icar_q11]
##player computing
###compute test scores
for i in range(len(answers)):
if submission[i] == answers[i]:
player.test_score += 1
def compute_bonus(player):
participant = player.participant
# payoff
## correct answers
answers = [4, 3, 5, 2, 2, 4, 5, 3, 1, 4, 5]
##get players answers
submission = [player.icar_q1, player.icar_q2, player.icar_q3, player.icar_q4, player.icar_q5, player.icar_q6,
player.icar_q7, player.icar_q8, player.icar_q9, player.icar_q10, player.icar_q11]
### get players correct answers
correct_total = []
correct = 0
for i in range(len(answers)):
if submission[i] == answers[i]:
correct += 1
correct_total.append(correct)
correct = 0
## select random numbers
group_of_items = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} # a sequence or set will work here.
num_to_select = 3 # set the number to select here.
participant.test_random = random.sample(list(group_of_items), num_to_select)
## compute player payoff
result= correct_total[participant.test_random[0]] + correct_total[participant.test_random[1]] + correct_total[participant.test_random[2]]
participant.bonus_score= result
participant.bonus_money= participant.bonus_score*.50
def icar_q1_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q2_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q3_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q4_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q5_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q6_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q7_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q8_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q9_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q10_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
def icar_q11_error_message(player, value):
if value not in [1, 2, 3, 4, 5, 6, 7, 8]:
return 'You have to select one of the options.'
# PAGES
class Instructions(Page):
@staticmethod
def before_next_page(player, timeout_happened):
player.time_start = round(time.time(),3)
class Test(Page):
form_model = 'player'
form_fields = ['icar_q1','icar_q2','icar_q3','icar_q4','icar_q5','icar_q6','icar_q7','icar_q8','icar_q9','icar_q10','icar_q11',]
timeout_seconds = 360
@staticmethod
def before_next_page(player, timeout_happened):
player.time_end = round(time.time(),3)
compute_score(player)
compute_bonus(player)
class Results(Page):
pass
page_sequence = [Instructions, Test, Results]