Challenge - 5 Problems
Flask Control Structures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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 %}Attempts:
2 left
💡 Hint
Look at the if condition inside the for loop and which item it highlights.
✗ Incorrect
The template loops over each item. Only when the item is 'banana' it wraps it in tags. Others are plain list items.
📝 Syntax
intermediate2: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 %}
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 %}Attempts:
2 left
💡 Hint
Check Jinja2 syntax for control structures carefully.
✗ Incorrect
Jinja2 uses {% endif %} to close if blocks and {% endfor %} to close for loops. The given code correctly closes both blocks. No colon is needed in Jinja2 tags. The 'for ... of' syntax is invalid in Jinja2.
❓ state_output
advanced2: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 %}
Flask
{% raw %}
<ul>
{% for n in numbers %}
{% if n % 2 == 0 %}
<li>{{ n }}</li>
{% endif %}
{% endfor %}
</ul>
{% endraw %}Attempts:
2 left
💡 Hint
Count how many numbers in the list are even.
✗ Incorrect
The template renders a list item only if the number is even. From 1 to 5, the even numbers are 2 and 4, so 2 list items are rendered.
🔧 Debug
advanced2: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 %}
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 %}Attempts:
2 left
💡 Hint
Check how Jinja2 accesses dictionary keys vs attributes.
✗ Incorrect
Jinja2 treats dot notation as attribute access. Since dictionaries don't have attributes, it raises UndefinedError when trying to access a missing attribute. KeyError is not raised because dot notation does not access keys directly.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about how nested loops and conditions work in normal programming.
✗ Incorrect
Jinja2 processes templates top to bottom. For nested loops and conditions, the outer loop runs first. For each item, the inner if condition is checked before rendering that item. This is standard control flow behavior.