from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ This is a one-shot "Prisoner's Dilemma". Two players are asked separately whether they want to cooperate or defect. Their choices directly determine the payoffs. """ class Constants(BaseConstants): name_in_url = 'd_prisoners_dilemma1' players_per_group = None num_rounds = 1 instructions_template = 'd_prisoners_dilemma1/Instructions.html' CC_payoff = c(6) CD_payoff = c(0) DC_payoff = c(10) DD_payoff = c(2) class Subsession(BaseSubsession): def creating_session(self): for player in self.get_players(): player.choice_random = random.randint(1, 10) print('player choice random', player.choice_random) class Group(BaseGroup): pass class Player(BasePlayer): choice_random = models.IntegerField() guessp = models.IntegerField( choices=[ [1,'Human'], [0,'Computer'], ], doc="""This player's guess""", widget=widgets.RadioSelectHorizontal ) message = models.LongStringField() decision = models.StringField( choices=['C', 'D'], doc="""This player's decision""", widget=widgets.RadioSelect ) guessPD_payoff = models.IntegerField() def set_guess_payoff(self): self.guessPD_payoff = 5 * (1-self.guessp) def set_payoff(self): if self.decision == "C" and self.choice_random<=5: self.payoff = self.guessPD_payoff + 6 if self.decision == "C" and self.choice_random>=6: self.payoff = self.guessPD_payoff + 0 if self.decision == "D" and self.choice_random<=5: self.payoff = self.guessPD_payoff + 10 if self.decision == "D" and self.choice_random>=6: self.payoff = self.guessPD_payoff + 2 self.participant.vars['Post2'] = self.payoff