from otree.api import * # ====================================================================================================================== doc = """ End of the experiment. Survey and demographic results. """ # ====================================================================================================================== # 1. Defining Constants # ====================================================================================================================== class Constants(BaseConstants): name_in_url = 'end' players_per_group = None num_rounds = 1 class Player(BasePlayer): # Creating survey fields survey_age = models.IntegerField(label="What is your age? (must specify a number)", min=15, max=65) survey_gender = models.StringField(label="What is your gender", choices=['Male', 'Female', 'Other', 'Prefer not to say']) survey_major = models.StringField(label="Which of the following best describes your major of study?", choices=['Business and Economics', 'Engineering and Sciences', 'Other']) survey_econ = models.IntegerField(label="How many Economics classes have you taken so far? (must specify a number)", min=0, max=32) survey_unis = models.IntegerField(label="How many years of university study have you completed (enter 0 if in first year)? (must specify a number)", min=0, max=10) survey_gpa = models.FloatField(label="What is your university GPA? (must specify a number)", min=0, max=7) # Create a field for the payoffs total_payoff = models.FloatField(initial=0.0) total_payoff_rounded = models.FloatField(initial=0.0) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass # ====================================================================================================================== # 2. Defining functions # ====================================================================================================================== # ====================================================================================================================== # 3. Defining Pages # ====================================================================================================================== class d1_survey(Page): form_model = Player form_fields = ['survey_age', 'survey_gender', 'survey_major', 'survey_econ', 'survey_unis', 'survey_gpa'] class d2_results(Page): @staticmethod def vars_for_template(player: Player): print(player.participant.d_payoff) exchange_rate = 50 participation_rate = 5 # Summarise the total payoff from the dictionary total_payoff = 0 for key in player.participant.d_payoff.keys(): print('the key is: ', key) if key[0:6] == 'payoff': total_payoff += player.participant.d_payoff[key] # Calculate the payoff from the comprehension question round comprehension_payoff = max(5 - max((1 * player.participant.questions - 1), 0), 0) # Calculate the total payoff player.total_payoff = round(total_payoff / exchange_rate + comprehension_payoff + participation_rate, 2) import math player.total_payoff_rounded = math.ceil(player.total_payoff * 5) / 5 # Return values return player.participant.d_payoff | dict(participation_rate = participation_rate, total_payoff=total_payoff, total_payoff_currency=round(total_payoff/exchange_rate, 2), comprehension_payoff=comprehension_payoff) page_sequence = [d1_survey, d2_results]