Sometimes you want to answer a question with some logic rather than directly asking the user for information.
Docassemble uses Python, a powerful but easy to read programming
language to control logic inside an interview. Python statements go inside a
In the example above, we introduced the use of the
| line continuation marker,
or vertical pipe. We always use this when the text that follows could go on
multiple lines, and to handle special characters (like accented letters). You
also always use it for a
We also introduced the
subquestion is used for longer
explanation text on a screen. It uses a smaller font that is not bold.
Sometimes you don't want to use logic to create a new variable. You can use
simple Python code right inside a
subquestion block, although the
syntax is very slightly different.
Notice that inside a
question block, the line with the
if statement starts
%. We also need to use an
endif statement, instead of using indentation
to show the beginning and end of the
Note: YAML is picky about indentation. If you run into an error, check to make sure each line is indented the same way as the example above.
Python allows us to go a little further than using just if-then. We can introduce
multiple branches using
Note: once a condition is satisfied in an if-then block of logic, Python moves on and stops the evaluation process. What do you think the result of this would be if the user puts "Bones" as their name?
- Modify the Logic exercise so that a new secret message is displayed when a name of your choice is displayed.
- Stretch goal: make all of the secret messages work regardless of how the user
capitalizes their name. Hint: using the
.lower()method of a Python string should help you out.
Quinten Steenhuis, June 2020