oTree Forum >

"otree.templating.errors.UndefinedVariable"

#1 by Lorenzo

Dear all, 
First of all,I must say I'm new in oTree without a real programming background. I'm trying to develop a difficult task in oTree. Now I have an error message which I am not able to solve:
" otree.templating.errors.UndefinedVariable: Cannot resolve the variable 'n_stock' (line 7, in "n_stock") "
This happened when I added 4 pages to my sequence instead of 1 there was before. I need this 5 identical pages because i need 5 rounds of this "decision task" , after this there is 1 time the result page and i need this process for 30 rounds. 

Thank you in advance.

#2 by Jonas

Hey Lorenzo,
i think we need to see the relevant parts of your code to be able to solve your problem :)

#3 by Lorenzo

Hi Jonas :) Thank you for your reply. I attach screenshots of the script. I hope this can help.

#4 by Lorenzo

Here:

#5 by Furuneko

Hi,

You need to add the method "vars_for_template" of page Decision5 also to the other pages.

Unless that's intentional, you need to have a different Player field for each Decision page, e.g. choice_1, choice_2, etc...

Best

#6 by Lorenzo

Hi Furuneko,

Regarding the second point. Do you mean that i have to write a new "class Player(BasePlayer)" for each decision page or to rename :"form_model = 'player' AND form_fields = ['choice']" with numbers?

Thank you,
Lorenzo

#7 by Furuneko

Hi,

You would need to add extra fields to the (same) Player class:

class Player(BasePlayer):
    choice_1 = models.IntegerField()
    choice_2 = models.IntegerField()
    choice_3 = models.IntegerField()
    ...
    
Then in the form_fields of each page you would need to add the relevant field - e.g.
class Decision1(Page):
    form_fields = ['choice_1']
    

class Decision2(Page):
    form_fields = ['choice_2']
    
And so on...

I hope that this helps.

#8 by Lorenzo

Thank you for this, I tiped everything. But now I have another error message and I don’t understand which is the mistake. 
"  File "..../__init__.py", line 65
    class Decision2(Page):
    ^
SyntaxError: invalid syntax
"

#9 by Furuneko

It is a syntax error, it would be useful if you could share your init.py.
(if I may, it seems to me that you would benefit from using oTree Studio, which would help you to avoid many of the errors that you are dealing with).

#10 by Lorenzo

Thank you for the suggestion. I try to use it.
I share the init.py :)

#11 by Lorenzo

Thank you all :) I solved my problem!

Write a reply

Set forum username