from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'Guess_2_3' PLAYERS_PER_GROUP = 5 NUM_ROUNDS = 4 class Subsession(BaseSubsession): pass class Group(BaseGroup): two_thirds_mean=models.FloatField() class Player(BasePlayer): number= models.FloatField( min=0, max=100, label="Your Number:" ) distance=models.FloatField() # PAGES class Instructions(Page): @staticmethod def is_displayed(player): return player.round_number == 1 class MyPage(Page): form_model = "player" form_fields = ["number"] def Calculate_Two_Thirds(group: Group): list_of_numbers=[p.number for p in group.get_players()] group.two_thirds_mean=sum(list_of_numbers)/len(list_of_numbers)*2/3 class ResultsWaitPage(WaitPage): after_all_players_arrive = Calculate_Two_Thirds class Results(Page): def vars_for_template(player: Player): list_of_other_players_guesses = [p.number for p in player.get_others_in_group()] list_of_other_players_ids = [p.id_in_group for p in player.get_others_in_group()] list_of_all_players_numbers=[p.number for p in player.group.get_players()] list_of_all_players_distances=[] for elem in list_of_all_players_numbers: list_of_all_players_distances.append(abs(elem-player.group.two_thirds_mean)) sorted_list_of_all_players_distances=sorted(list_of_all_players_distances) player.distance=abs(player.number-player.group.two_thirds_mean) dict_guesses_two_thirds = { f"{list_of_other_players_ids[0]}": list_of_other_players_guesses[0], f"{list_of_other_players_ids[1]}": list_of_other_players_guesses[1], f"{list_of_other_players_ids[2]}": list_of_other_players_guesses[2], f"{list_of_other_players_ids[3]}": list_of_other_players_guesses[3], f"{player.id_in_group}": player.number, "Two Thirds of Average": player.group.two_thirds_mean, } list_guesses_two_thirds=sorted(dict_guesses_two_thirds.items(), key=lambda x:x[1]) return{ "list_of_other_players_guesses": list_of_other_players_guesses, "list_of_other_players_ids": list_of_other_players_ids, "list_of_all_players_distances": list_of_all_players_distances, "Value_0": list_guesses_two_thirds[0][1], "Value_1": list_guesses_two_thirds[1][1], "Value_2": list_guesses_two_thirds[2][1], "Value_3": list_guesses_two_thirds[3][1], "Value_4": list_guesses_two_thirds[4][1], "Value_5": list_guesses_two_thirds[5][1], "Key_0": list_guesses_two_thirds[0][0], "Key_1": list_guesses_two_thirds[1][0], "Key_2": list_guesses_two_thirds[2][0], "Key_3": list_guesses_two_thirds[3][0], "Key_4": list_guesses_two_thirds[4][0], "Key_5": list_guesses_two_thirds[5][0], "mean_guess": sum(list_of_all_players_numbers) / len(list_of_all_players_numbers), "place": 1 if player.distance==sorted_list_of_all_players_distances[0] else 2 if player.distance==sorted_list_of_all_players_distances[1] else 3 if player.distance == sorted_list_of_all_players_distances[2] else 4 if player.distance == sorted_list_of_all_players_distances[3] else 5 if player.distance == sorted_list_of_all_players_distances[4] else "Error", } page_sequence = [Instructions, MyPage, ResultsWaitPage, Results]