简体   繁体   English

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

[英]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? 这是怎么回事?

Just use set before the sum += 只需在sum +=之前使用set

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM