oTree Forum >

TypeError happens only in some treatments.

#1 by Aysegul_Engin

Hello everyone. 

I am writing an experiment where the participant chooses between 2 lotteries and receives feedback in the next round. The experiment is set to have 3 treatments and I am coding all treatments in one app. To inform participants about the outcome in round = n I am calling the set_outcome method in round = n before next page. 

In the base treatment this workflow works perfectly. However in the other 2 treatments I receive a Type error. (TypeError: player.outcome is None.) Can anyone help me understand why this is happening? 

The minimal code is below: 

class C(BaseConstants):
    REWARD = 100 # in the storyline points won for each lottery
    
def creating_session(subsession: Subsession):
    # Set balanced treatments
    import itertools
    treatments = itertools.cycle(["Base", "Recommender", "DSS"])
    for player in subsession.get_players():
        player.treatment = next(treatments)

class Player(BasePlayer):
    treatment = models.StringField(doc = "The treatment variable")
    outcome = models.IntegerField(doc = "The outcome of the round for the chosen lottery")
    
def set_outcome(player):
    outcomes = [C.REWARD, 0]
    if player.choice == "A":
        outcome = random.choices(outcomes, weights = (player.lottery_A_success_prob, 1-player.lottery_A_success_prob), k=1)
    else:
        outcome = random.choices(outcomes, weights = (player.lottery_B_success_prob, 1-player.lottery_B_success_prob), k=1)

    player.outcome = int(outcome[0])
    return player.outcome
    
def vars_for_template1(player:Player):
    for p in player.subsession.get_players():
        if p.round_number > 1:
            return dict(previous_outcome = p.in_round(p.round_number - 1).outcome)
            
class Choice(Page):
    form_model = "player"
    form_fields = ["choice"]

    @staticmethod
    def before_next_page(player, timeout_happened):
        set_outcome(player)
        set_recommendation(player)
        set_payoff(player)
        if player.round_number < C.NUM_ROUNDS:
            set_counts(player)

    vars_for_template = vars_for_template1
    
    
According to the error message the line `return dict(previous_outcome = p.in_round(p.round_number - 1).outcome)` is creating the problem. However I don't understand why it works in the base treatment. 

Thank you vey much for your help. 
Ayşegül

#2 by Aysegul_Engin

Maybe one small additional information. The treatments differ only on the basis of the templates. 

The template, where the treatments are making a difference seem like in the following code: 

{{ block title }}
    Round {{ player.round_number }} of {{ C.NUM_ROUNDS }}
{{ endblock }}
{{ block content }}

    {{ if subsession.round_number < 11 }}
        <p>
            <b> This is a tryout round! </b>
        </p>
    {{ endif }}

    {{ if subsession.round_number > 1 }}
        {{if player.treatment == "Base"}}
            <p>Your outcome in the last round is {{ previous_outcome }}.</p>
        {{ else }}
            {{if player.treatment == "Recommender"}}
                <p>Your outcome in the last round is {{ previous_outcome }}.</p>
                <p>Based on your past choices, the computer recommends you to choose: <b>{{player.recommendation}}</b></p>
            {{ else }}
                {{if player.treatment == "DSS"}}
                    <p>Your outcome in the last round is {{ previous_outcome }}.</p>
                    <p>The table below shows the number of times that lotteries yielded prize according to your choices. </p>

                        <table class="table">
                                <tr>
                                    <th> Lottery A </th>
                                    <th> Lottery B </th>

                                </tr>
                                <tr>
                                    <td>{{player.non_zero_outcome_count_A}}</td>
                                    <td>{{player.non_zero_outcome_count_B}}</td>
                                </tr>

                            </table>
                {{ endif }}
            {{ endif }}
        {{ endif }}
    {{ endif }}

    {{ formfields }}
    {{ next_button }}
    
    
 Apart from these differences in all treatments everything is the same.

Write a reply

Set forum username