Int64 to Int on Otree survey

I’m trying to host an Otree survey but I keep getting an error directed at this line of code that says “relevant_index should be set to int, not int64.”

I have tried to use a.astype(int) but I’m still getting the error… can someone help me?

def set_treatment_vars(self):
        # Set the Answers of the Social Group
        if self.treatment == 'IND':
            self.A1 = 4
            self.A2 = 4
            self.A3 = 1
            self.A4 = 2
            self.A5 = 2
        elif self.treatment == 'COM':
            self.A1 = 3
            self.A2 = 3
            self.A3 = 3
            self.A4 = 3
            self.A5 = 3
        # Determine the Payoff Relevant Question and Answer
        self.relevant_index = np.random.choice(range(0, 5, 1))
        self.relevant_question = Constants.questions[self.relevant_index]
        answers = [self.A1, self.A2, self.A3, self.A4, self.A5]
        self.correct_answer = answers[self.relevant_index]

You could try:

self.relevant_index = int(np.random.choice(range(0, 5, 1)))

which, incidentally, can be simplified to:

self.relevant_index = int(np.random.choice(range(5)))

Thank you ! This helped; I am getting a “list index out of range” error now when I try to run this line

        self.relevant_question = Constants.questions[self.relevant_index]

I’m assuming one of the solutions is to use range()?

Thank you again!

Instead of giving range a fixed value of 5, which assumes that Constants.questions has at least 5 members, give it len(Constants.questions).

Hm doing this gives me the " ‘int’ object is not subscriptable" error.

self.relevant_index = int(np.random.choice(range(0, 5, 1)))
        self.relevant_question = len(Constants.questions)[self.relevant_index]
        answers = [self.A1, self.A2, self.A3, self.A4, self.A5]
        self.correct_answer = answers[self.relevant_index]

No, I said to replace the fixed value of 5 with len(Constants.questions).

Okay so you’re saying something like this?

self.relevant_index = int(np.random.choice(range(len(Constants.questions))))
  self.relevant_question = Constants.questions[self.relevant_index]
        answers = [self.A1, self.A2, self.A3, self.A4, self.A5]
        self.correct_answer = answers[self.relevant_index]

Yes, exactly.

Perfect, thank you so much for your help!! it worked :slight_smile: