External Exam Download Resources Web Applications Games Recycle Bin

server.py

from flask import *
app = Flask(__name__)

scores = [11, 7, 9, 10, 13, 11, 10, 9]

@app.route("/")
def main():
    return render_template("template.html",
                           scores = scores)
 
app.run(debug=True)

templates\template.html

<!-- using namespace() as vars change in for loop: -->
{% set variables = namespace(total=0, max=0, count=0) %}
<!-- for each score: -->
{% for s in scores %}
  <!-- print s: -->
  {{s}}<br>
  <!-- add to total: -->
  {% set variables.total = variables.total + s %}
  <!-- check max: -->
  {% if s > variables.max %}
    {% set variables.max = s %} 
  {% endif %}
  <!-- set count to built-in loop.index: -->
  {% set variables.count = loop.index %}
{% endfor %}
<!-- calc average: -->
{% set average = variables.total / variables.count %}
<!-- print all vars: -->
first: {{scores[0]}}<br>
last: {{scores[scores|length-1]}}<br>
total: {{variables.total}}<br>
max: {{variables.max}}<br>
count: {{variables.count}}<br>
average: {{average}}