Getting started with programming in oTree
For programming newbs:
- Don’t worry if you are new to Python. All things you need to know to get started are:
- data types
- especially,
str
,int
,float
,bool
,list
,dict
- especially,
- operators
- algebraic:
+
(addition),-
(subtraction),*
(multiplication),/
(division),//
(quotient),%
(modulus),**
(power) - comparison/logical:
==
,!=
,>
,<
,>=
,<=
,and
,or
,not
- algebraic:
for
loop- useful in repeating something (e.g., repeatedly calculating the payoff of group members). For example:
players = [player1, player2, player3] for player in players: if player.winner == True: player.payoff = 100 else: player.payoff = 0
- functions
import
-ing modulesrandom
modulerandom.randint()
orrandom.randrange()
,random.random()
,random.choice()
, andrandom.choices()
are most used methods.
- data types
-
Additionally, you need to know some basic HTML tags:
<img>
(to include image to your experiment webpage),<p>
(to write texts),<h1>
,<h2>
.. (headlines) - Programming is like taking an exam with a cheatsheet and a tutor sitting next to you. You don’t need to memorize anything. The only skills you need to be a programmer are the ability to google what you don’t know and the ability to read error messages.
oTree documentation
-
The latest oTree documentation gives all the information you need to know to program in oTree.
-
Shortcut:
(1) Follow Installing oTree -> Tutorial Part 1
(2) Read through Conceptual overview -> Models, Pages, and Templates
(3) Follow Tutorial Part 2 and Part 3 and search for whatever you don’t know comes up
Some tips
-
Ask questions on oTree Forum
-
Looking at demo codes will be helpful. You can even find codes of what you wanted to program.
-
Debugging: when trying your oTree app locally, you can
print
variables/data to see if the program is working. You will see the output in the command line.
Last updated: October 12, 2022 by Seura Ha