【问题标题】:Local Variable Scope in Tornado (python)Tornado 中的局部变量范围(python)
【发布时间】:2012-08-15 02:04:47
【问题描述】:

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

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

发生了什么事?

【问题讨论】:

    标签: python html templates tornado


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-07-14
      • 1970-01-01
      • 2016-11-13
      • 2013-05-10
      • 2022-01-12
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多