oTree Forum >

"Player" is not defined: Otree VScode issue

#1 by ecopsy2023

Hello there, 
I am just starting out with oTree trying to make a repeated game. While testing some seemingly simple code I have ran into an error which stated: '"player" is not defined' 

The code in question is: 
class Player(BasePlayer):


    def vars_for_template(player):
    current_round = player.round_number

    income_vector = [1,2,3,4,5,6,7,8,9,10]
    income = player.income_vector(current_round-1)

    consumption = models.IntegerField()
    
    
 From the oTree Documentation I believed player.variablename would work. Am I incorrect? Does anyone have any advice on what to do to fix this?

#2 by Fanist

Hi, I think there might be a misunderstanding that we only define what variables we want under the Player Class.

So in your code:
consumption = models.IntegerField(),
this line defines your "consumption" and should be put under Player. 

However, other codes:

 def vars_for_template(player):
    current_round = player.round_number

    income_vector = [1,2,3,4,5,6,7,8,9,10]
    income = player.income_vector(current_round-1)
    
Put them under your "Page", because the function vars_for_template() runs when people enter the page. And there are some errors that if you want to store the "income" in Player, you should first define them like the "consumption" do. 

Finally, the code should be like this:

class Player(BasePlayer):
    # Define variables we want to store:
    # two integer variables: consumption and income 
    consumption = models.IntegerField()
    income = models.IntegerField(min = 1, max = 10) # we limit the range of the variable
    
class Your_Page_Name(Page):
    # when people come to this page, the function vars_for_template() runs once automatically 
    @staticmethod
    def vars_for_template(player: Player):
        # get the current round from the Player field
        # we do not define "round_number" in Player since it is a default variable to access
        current_round = player.round_number
        # define a fixed income vector for the next step
        income_vector = [1,2,3,4,5,6,7,8,9,10]
        # We store values into "income" in the Player field, which is written as player.income
        player.income = income_vector(current_round-1)


Hope that helps!

#3 by ecopsy2023

Hey there, 
Thank you so much for your response. I made the changes you suggested and definitely now understand why my code wouldn't work!

Write a reply

Set forum username