oTree Forum >

Null field Issues

#1 by caroolin

I have this: 

from otree.api import Currency, models, BaseConstants, BaseSubsession, BaseGroup, BasePlayer
from otree.api import Page, WaitPage

doc = """
Market Entry Game
"""


class C(BaseConstants):
    NAME_IN_URL = 'market_game'
    PLAYERS_PER_GROUP = 2
    NUM_ROUNDS = 6


class Subsession(BaseSubsession):
    market_condition = models.StringField()

    def creating_session(self):
        print("creating_session is being called")  # Debug print
        condition_sequence = ['Favorable', 'Unfavorable', 'Moderate']
        round_index = (self.round_number - 1) % 3
        self.market_condition = condition_sequence[round_index]
        print("Market condition set to:", self.market_condition)


class Group(BaseGroup):
    def set_payoffs(self):
        for p in self.get_players():
            p.set_payoff()


class Player(BasePlayer):
    decision = models.StringField(choices=['Enter', 'Hold'])

    def set_payoff(self):
        other_player = self.get_others_in_group()[0]
        condition = self.subsession.market_condition
        decision = self.decision
        other_decision = other_player.decision

        if condition == 'Favorable':
            if decision == 'Enter' and other_decision == 'Hold':
                self.payoff = Currency(100)
                other_player.payoff = Currency(10)
            elif decision == 'Hold' and other_decision == 'Enter':
                self.payoff = Currency(10)
                other_player.payoff = Currency(100)
            elif decision == 'Enter' and other_decision == 'Enter':
                self.payoff = Currency(50)
                other_player.payoff = Currency(50)
            elif decision == 'Hold' and other_decision == 'Hold':
                self.payoff = Currency(10)
                other_player.payoff = Currency(10)
        elif condition == 'Unfavorable':
            if decision == 'Enter' and other_decision == 'Hold':
                self.payoff = Currency(-5)
                other_player.payoff = Currency(10)
            elif decision == 'Hold' and other_decision == 'Enter':
                self.payoff = Currency(10)
                other_player.payoff = Currency(-5)
            elif decision == 'Enter' and other_decision == 'Enter':
                self.payoff = Currency(-10)
                other_player.payoff = Currency(-10)
            elif decision == 'Hold' and other_decision == 'Hold':
                self.payoff = Currency(10)
                other_player.payoff = Currency(10)
        elif condition == 'Moderate':
            if decision == 'Enter' and other_decision == 'Hold':
                self.payoff = Currency(40)
                other_player.payoff = Currency(10)
            elif decision == 'Hold' and other_decision == 'Enter':
                self.payoff = Currency(10)
                other_player.payoff = Currency(40)
            elif decision == 'Enter' and other_decision == 'Enter':
                self.payoff = Currency(20)
                other_player.payoff = Currency(20)
            elif decision == 'Hold' and other_decision == 'Hold':
                self.payoff = Currency(10)
                other_player.payoff = Currency(10)


# PAGES
class MarketConditionPage(Page):
    @staticmethod
    def vars_for_template(player):
        print(player)
        return {'market_condition': player.subsession.market_condition}


class DecisionPage(Page):
    form_model = 'player'
    form_fields = ['decision']


class ResultsWaitPage(WaitPage):
    after_all_players_arrive = 'set_payoffs'


class Results(Page):
    @staticmethod
    def vars_for_template(player):
        return {
            'my_decision': player.decision,
            'other_player_decision': player.get_others_in_group()[0].decision,
            'my_payoff': player.payoff,
            'other_player_payoff': player.get_others_in_group()[0].payoff,
            'market_condition': player.subsession.market_condition
        }


page_sequence = [MarketConditionPage, DecisionPage, ResultsWaitPage, Results]


And get this when running the otree devserver:
TypeError: subsession.market_condition is None. Accessing a null field is generally considered an error. Or, if this is intentional, use field_maybe_none()

But I do not want none, but the game to start with my sequence.
Anybody know how to adapt?

Thank you!
Best,
Carolin

#2 by Fanist

Are you using the new version of o-Tree? If so, the usage of creating_session() seems incorrect. Put all functions (creating_session(), set_payoffs ..) outside the Class and let them be standalone. You can add a print in the function to see whether it is triggered when running the code.

Write a reply

Set forum username