0
0
Flaskframework~20 mins

Control structures (if, for) in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Control Structures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Flask template render?
Given this Flask Jinja2 template snippet, what will be the rendered HTML output if items = ['apple', 'banana', 'cherry']?
Flask
{% raw %}
<ul>
  {% for item in items %}
    {% if item == 'banana' %}
      <li><strong>{{ item }}</strong></li>
    {% else %}
      <li>{{ item }}</li>
    {% endif %}
  {% endfor %}
</ul>
{% endraw %}
A<ul><li>apple</li><li><strong>banana</strong></li><li>cherry</li></ul>
B<ul><li><strong>apple</strong></li><li>banana</li><li><strong>cherry</strong></li></ul>
C<ul><li><strong>apple</strong></li><li><strong>banana</strong></li><li><strong>cherry</strong></li></ul>
D<ul><li>apple</li><li>banana</li><li>cherry</li></ul>
Attempts:
2 left
💡 Hint
Look at the if condition inside the for loop and which item it highlights.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Flask template code
Which option correctly fixes the syntax error in this Flask Jinja2 template snippet?
{% raw %}
    {% for user in users %} {% if user.active %}
  • {{ user.name }}
  • {% endif %} {% endfor %}
{% endraw %}
Assume users is a list of user objects with an active boolean attribute.
Flask
{% raw %}
<ul>
  {% for user in users %}
    {% if user.active %}
      <li>{{ user.name }}</li>
    {% endif %}
  {% endfor %}
</ul>
{% endraw %}
ANo change needed; the code is correct as is.
BAdd a colon after {% if user.active %} like {% if user.active: %}.
CReplace {% endif %} with {% endfor %} to close the for loop.
DReplace {% for user in users %} with {% for user of users %}.
Attempts:
2 left
💡 Hint
Check Jinja2 syntax for control structures carefully.
state_output
advanced
2:00remaining
What is the output count of list items rendered?
Given this Flask Jinja2 template snippet and numbers = [1, 2, 3, 4, 5], how many <li> elements will be rendered?
{% raw %}
    {% for n in numbers %} {% if n % 2 == 0 %}
  • {{ n }}
  • {% endif %} {% endfor %}
{% endraw %}
Flask
{% raw %}
<ul>
  {% for n in numbers %}
    {% if n % 2 == 0 %}
      <li>{{ n }}</li>
    {% endif %}
  {% endfor %}
</ul>
{% endraw %}
A3
B2
C5
D0
Attempts:
2 left
💡 Hint
Count how many numbers in the list are even.
🔧 Debug
advanced
2:00remaining
Why does this Flask template raise an error?
Consider this Flask Jinja2 template snippet:
{% raw %}
    {% for item in items %} {% if item.is_active %}
  • {{ item.name }}
  • {% endif %} {% endfor %}
{% endraw %}
If items is a list of dictionaries without the key is_active, what error will occur?
Flask
{% raw %}
<ul>
  {% for item in items %}
    {% if item.is_active %}
      <li>{{ item.name }}</li>
    {% endif %}
  {% endfor %}
</ul>
{% endraw %}
ATypeError: 'list' object is not callable
BKeyError: 'is_active'
CUndefinedError: 'dict object' has no attribute 'is_active'
DNo error; renders empty list items.
Attempts:
2 left
💡 Hint
Check how Jinja2 accesses dictionary keys vs attributes.
🧠 Conceptual
expert
3:00remaining
Which option best describes Jinja2's control flow evaluation order?
In Flask's Jinja2 templates, when rendering nested control structures like for loops and if statements, which statement is true about the evaluation order?
AJinja2 compiles the entire template and renders all <code>if</code> blocks before any <code>for</code> loops.
BThe <code>if</code> statements run independently of <code>for</code> loops and do not affect iteration.
CAll <code>if</code> conditions are evaluated first for all items, then the <code>for</code> loop renders the filtered results.
DThe outer <code>for</code> loop runs first, then for each iteration the inner <code>if</code> condition is evaluated before rendering content.
Attempts:
2 left
💡 Hint
Think about how nested loops and conditions work in normal programming.