oTree Forum >

oTree PyCharm tutorial part 2 error

#1 by js8773420 (edited )

Hi everyone,

I am getting an issue with following part 2 of the tutorial, in the def set_payoffs(self) part. In the results html page I put in: 

Your group contributed {{ group.total_contribution }} resulting in an individual share of {{ group.individual_share }}.

However, when I run the server, this comes back with an error as seen in the first screenshot. I have tested it using the print function to see that group.total_contribution (and group.individual_share) comes back with the error: "NameError: name 'group' is not defined" and when I change it to Group.total contibution it comes back with "Group.total_contribution is <function CurrencyField at 0x000002AF101595A0>" implying to me that it is relying on the initial total_contribution = models.CurrencyField function, not what is under the def set_payoffs(self) part. 

I am new to oTree so maybe I'm missing something obvious but is anyone familiar with why this is happening and how I can fix it?

Additionally,  "print(self.Group.total_contribution)" and "print(self.Group.total_contribution)" comes back with "NameError: name 'self' is not defined" (also occurs when I use a lowercase g), and "print(Group.self.total_contribution)" comes back with "AttributeError: type object 'Group' has no attribute 'self'".

#2 by aseyq

Hi there,

I went through the tutorial and I cannot reproduce it. Do you mind sharing your `__init__.py`? Also can you check your oTree version either by `otree --version` on Terminal, or on the Server Check page.

Also, it would help if you could tell where you put the print statement. When you use capitalized Group, it refers to the class itself, rather than the group object. So I wouldn't worry much about it.

#3 by js8773420

Thanks aseyq,

I've attached my _init_.py file, I put the print statement just after the def set_payoffs(self) part. Using the 'otree --version' command tells me that my version is 5.8.5. Is that the correct one?

#4 by aseyq

Hello js!,
Yes that seems to be the latest oTree. Comparing my own code, I spotted a few errors. I am attaching the fixed version.

- The main issue was that parentheses were missed in currency definitions (line 21):

```
class Group(BaseGroup):
    total_contribution = models.CurrencyField()
    individual_share = models.CurrencyField()
```

- Another thing I noticed was that the endowment and the multiplier constants were defined as lowercase variables. That's of course fine but in the HTML template, they are uppercase (ENDOWMENT and MULTIPLIER). I turned these into uppercase variables.

- And finally, somehow set_payoff function syntax was mixed with the old `self` format and the current format where the function should get `group` as an input and do things with it. Here is how it should look:

```
def set_payoffs(group):
    players = group.get_players()
    contributions = [p.contribution for p in players]
    group.total_contribution = sum(contributions)
    group.individual_share = group.total_contribution * C.MULTIPLIER / C.PLAYERS_PER_GROUP
    for player in players:
        player.payoff = C.ENDOWMENT - player.contribution + group.individual_share    
    print("Group.total_contribution", group.individual_share)
```

I should note that most of these are mistakes that I too am likely to make too. It might be non-sense and painful at first but (like many other things) once it clicks, it is very enjoyable to work with oTree. I wish you a good start!

Best!

#5 by js8773420

Thanks aseyq!

Write a reply

Set forum username