from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import csv import random import numpy as np author = 'Your name here' doc = """ A quiz app that reads its questions from a spreadsheet (see quiz.csv in this directory). There is 1 question per page; the number of pages in the game is determined by the number of questions in the CSV. See the comment below about how to randomize the order of pages. """ class Constants(BaseConstants): name_in_url = 'my_quiz2' players_per_group = 2 with open('my_quiz2/quiz2.csv') as questions_file: questions = list(csv.DictReader(questions_file)) num_rounds = len(questions) multiplier = c(10) cost = c(2) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: # self.session.vars['questions'] = Constants.questions.copy() ## ALTERNATIVE DESIGN: ## to randomize the order of the questions, you could instead do: import random randomized_questions = random.sample(Constants.questions, len(Constants.questions)) self.session.vars['questions'] = randomized_questions ## and to randomize differently for each participant, you could use ## the random.sample technique, but assign into participant.vars ## instead of session.vars. # rand = np.array(random.sample(range(1, 10), 2)) # self.session.vars['rand10'] = rand[0] # self.session.vars['rand20'] = rand[1] # self.session.vars['rand11'] = np.random.choice([0, 1], p=[0.5, 0.5]) # self.session.vars['rand21'] = np.random.choice([0, 1], p=[0.5, 0.5]) # print('rand10', self.session.vars['rand10']) # print('rand20', self.session.vars['rand20']) # print('rand11', self.session.vars['rand11']) # print('rand21', self.session.vars['rand21']) for p in self.get_groups(): question_data = p.current_question() p.question_id = int(question_data['id']) p.question = question_data['question'] p.solution = question_data['solution'] class Group(BaseGroup): question_id = models.IntegerField() question = models.StringField() solution = models.StringField() submitted_answer = models.StringField(widget=widgets.RadioSelect) is_correct = models.BooleanField() init_grading = models.BooleanField() orig_grading = models.BooleanField() request = models.BooleanField() num_requests = models.IntegerField() regrade = models.BooleanField() fn_grading = models.BooleanField() init_correct = models.IntegerField() fn_correct = models.IntegerField() orig_correct = models.IntegerField() init_right = models.BooleanField() fn_right = models.BooleanField() initgrade_correct = models.IntegerField() fngrade_correct = models.IntegerField() confidence = models.FloatField( min=0, max=100, widget=widgets.Slider(attrs={'step': '1'}), label="How confident are you about your answer? 0 (Not at all) - 100 (Absolutely confident)." ) def current_question(self): return self.session.vars['questions'][self.round_number - 1] def check_correct(self): self.is_correct = (self.submitted_answer == self.solution) # self.orig_grading = self.is_correct # if self.session.vars['rand10'] == self.round_number or self.session.vars['rand20'] == self.round_number: # if self.session.vars['rand11'] == 0: # self.orig_grading = False # else: # self.orig_grading = True def fn_grade(self): if self.request == False: self.fn_grading = self.init_grading else: self.fn_grading = self.regrade def init_grading_right(self): self.init_right = (self.init_grading == self.is_correct) def fn_grading_right(self): self.fn_right = (self.fn_grading == self.is_correct) def set_payoffs(self): student = self.get_player_by_id(1) instructor = self.get_player_by_id(2) if self.fn_grading == True: if self.request == True: student.payoff = Constants.multiplier - Constants.cost else: student.payoff = Constants.multiplier else: student.payoff = c(0) if self.fn_right == True: instructor.payoff = Constants.multiplier else: instructor.payoff = c(0) class Player(BasePlayer): screen_name = models.StringField(label="What is your first name?") sex = models.StringField( choices=['Female', 'Male'], widget=widgets.RadioSelectHorizontal, label="What is your sex" ) partner_name = models.StringField() partner_sex = models.StringField() def get_partner(self): partner = self.get_others_in_group()[0] print(self.id_in_group) print(self.get_others_in_group()[0]) print(self.screen_name) print(self.sex) self.session.vars['screen_name'][0] = self.screen_name self.session.vars['sex'][0] = self.sex self.session.vars['partner_name'][1] = partner.screen_name self.session.vars['partner_sex'][1] = partner.sex