Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Variable Scope in Tornado (python)

just wondering what the deal is with the scope of variables in a tornado template. I have this code in a template, but it fails!

    {% set dictionary = {'a' : 1, ... more letters here ... 'z' : 26} %}
    {% set sum = 0 %}
    {% for y in x %}
        {% sum += int(dictionary[y.lower()]) #x is a string, y a char %}
    {% end %}
    {{ sum }}

But I get:

ParseError: unknown operator: 'sum'

What's going on?

like image 755
Jeremy Rubin Avatar asked Sep 23 '26 11:09

Jeremy Rubin


1 Answers

Just use set before the sum +=

{% set dictionary = {'a' : 1, ... more letters here ... 'z' : 26} %}
{% set sum = 0 %}
{% for y in x %}
    {% set sum += int(dictionary[y.lower()]) #x is a string, y a char %}
{% end %}
{{ sum }}
like image 141
Sharoon Thomas Avatar answered Sep 25 '26 01:09

Sharoon Thomas