龙卷风中的局部变量范围(python)

时间:2012-08-15 02:04:48

标签: python html templates tornado

只是想知道龙卷风模板中的变量范围是什么。我在模板中有这个代码,但它失败了!

    {% 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 }}

但我明白了:

ParseError: unknown operator: 'sum'

发生了什么事?

1 个答案:

答案 0 :(得分:7)

只需在set

之前使用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 }}