oTree Forum >

Card Selection

#1 by FEDx

Hello, everyone,
I need a player to select a card from a set of 21 cards drawn from a standard pack of 54 and win 1€ for each card of the same color as the chosen card in the pack of 21.

So far:

from otree.api import models

class Constants(BaseConstants):
    num_cards = 21
    card_colors = ['red', 'black']
    payoff_per_card = 1

class Subsession(BaseSubsession):

class Group(BaseGroup):

class Player(BasePlayer):
    chosen_card = models.StringField()

    def calculate_payoff(self):
        chosen_card = self.chosen_card
        cards_of_chosen_card = self.participant.vars['cards'].count(chosen_card)
        self.payoff = cards_of_chosen_card * Constants.payoff_per_card
class CardSelectionPage(Page):
    def vars_for_template(self):
        deck = Constants.card_colors * Constants.num_cards
        self.participant.vars['cards'] = deck[:Constants.num_cards]

        available_cards = list(set(self.participant.vars['cards']))

        return {'available_cards': available_cards}

class ResultsPage(Page):
    def vars_for_template(self):
        chosen_card = self.player.chosen_card
        cards_of_chosen_card = self.participant.vars['cards'].count(chosen_card)
        payoff = cards_of_chosen_card * Constants.payoff_per_card

        return {
            'chosen_card': chosen_card,
            'cards_of_chosen_card': cards_of_chosen_card,
            'payoff': payoff

    def before_next_page(self):
The problem is that I received an error [TypeError: 'tuple' object does not support item assignment]

How can I fix it?

#2 by Chris_oTree

What line of code does the error message refer to?

#3 by Chris_oTree

Can you show the full traceback

#4 by FEDx

I attached a screenshot. I think the problem is the {card_colors = ['red', 'black']} in the Constants because I created it as a tuple.

#5 by Chris_oTree

It looks to me like a list, but you can be sure by converting it to a list with list() before shuffling. Tuples cannot be shuffled.

Write a reply

Set forum username