#1 by caroolin
Hello! I want to programm the following: You will create an experiment, where your subject’s task is to calculate the sum of two randomly generated numbers between 1 and 100. If the calculation is correct, your subject gets 1 point, if it is false, she gets 0 points. You also need the random function, to generate two different random numbers for your player Hint: Generate the random numbers on your input page within the vars_for_template function (fine for us for now!) Hint: Assign the payoffs on your input page, by using the function: before_next_page(player, timeout_happened) I now have the following _init_ structure: from otree.api import * import random doc = """ Experiment where participants calculate the sum of two randomly generated numbers between 1 and 100. Participants earn 1 point for a correct answer and 0 points for an incorrect one. """ class Constants(BaseConstants): name_in_url = 'addition_task' players_per_group = None num_rounds = 1 class Subsession(BaseSubsession): def creating_session(self): for player in self.get_players(): player.number1 = random.randint(1, 100) player.number2 = random.randint(1, 100) class Group(BaseGroup): pass class Player(BasePlayer): number1 = models.IntegerField() number2 = models.IntegerField() sum_input = models.IntegerField() score = models.IntegerField(initial=0) class MyPage(Page): form_model = 'player' form_fields = ['sum_input'] def vars_for_template(self): return { 'number1': self.player.number1, 'number2': self.player.number2 } def before_next_page(self): correct_sum = self.player.number1 + self.player.number2 if self.player.sum_input == correct_sum: self.player.score = 1 else: self.player.score = 0 class Results(Page): def vars_for_template(self): return { 'is_correct': self.player.score == 1, 'correct_sum': self.player.number1 + self.player.number2 } page_sequence = [MyPage, Results] But it does not work and says the following: Unresolved attribute reference 'player' for class 'MyPage':40 Unresolved attribute reference 'player' for class 'MyPage':41 Signature of method 'MyPage.before_next_page()' does not match signature of the base method in class 'Page':44 Unresolved attribute reference 'player' for class 'MyPage':45 Unresolved attribute reference 'player' for class 'MyPage':45 Unresolved attribute reference 'player' for class 'MyPage':46 Unresolved attribute reference 'player' for class 'MyPage':47 Unresolved attribute reference 'player' for class 'MyPage':49 Unresolved attribute reference 'player' for class 'MyPage':55 Unresolved attribute reference 'player' for class 'MyPage':56 Unresolved attribute reference 'player' for class 'MyPage':56
#2 by Rok
Hello, In both your posts, this one and the Stag Game, the issues stem from a misunderstanding of the parameters for functions inside a page class. In older versions of oTree, these functions did indeed have a parameter named self. However, this has been outdated for a few years now. The newer versions of oTree use different parameters, usually player. (https://otree.readthedocs.io/en/latest/pages.html?highlight=vars_for_template#vars-for-template) In your code, you still write the function as vars_for_template(self), but 'self' is just a name you defined for the player object that is passed to this function automatically. When you call 'self.player' inside the function, it actually tries to get a player object out of a player object (player.player), which causes errors. Same for 'self.group', it should be 'player.group'. Rewrite your functions: rename the 'self' parameter to 'player', and use the 'player' parameter instead of 'self.player' in the functions. Another outdated concept is the creating_session function that you defined inside your subsession class. The creating_session function is now defined at the same level as other classes. You should delete the line 'class Subsession(BaseSubsession):' and indent your function one level to the left to align it with others. (https://otree.readthedocs.io/en/latest/treatments.html?highlight=creating_session#creating-session) Those might not be all the issues with your code, but it should get you started, and solve all of your existing errors. Best regards and good luck, Rok