Article
Calculated variables
When and Why to Use
Use these to save and retrieve values for use in survey logic or reporting.
-
store_value() lets you assign a value (like a derived age or score) to a named variable
-
get_value() retrieves that value later in the survey for use in branching, filtering, or display
Common use cases include:
-
Calculating age from birth year
-
Storing a derived score
store_value(name, value, tags)
| Parameter | Type | Required | Description |
|---|---|---|---|
| name | str | yes | Name to assign to the stored value |
| value | str or int | yes | Value to store, often calculated from a previous answer |
tags | s.tag() | no | Used for grouping and metadata in reporting |
get_value(name)
| Parameter | Type | Required | Description |
|---|---|---|---|
| name | str | yes | Name of the value to retrieve |
Returns: The stored value (any type)
Example Code
from datetime import datetime
from survey import Survey
s = Survey(**globals())
birth_year = s.numeric_question("What year were you born in?", min_max=(1920, 2020))
s.store_value("age", datetime.now().year - birth_year)
s.terminate_if( s.get_value("age") < 18, "Sorry, this survey is for adults 18 and older." )
Notes
-
store_value can be used multiple times to keep track of intermediate calculations
-
Values can be used in logic, shown in text, or passed to external APIs
-
You can apply tags for each stored value to categorize them in your reporting schema