Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Khoa Nguyen
Khoa Nguyen
1,781 Points

what's wrong with my code

Please help me check. thanks

lunch.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    class User:
        email = None
    user = User()
    user.email = 'kenneth@teamtreehouse.com'
    return render_template('user.html', user=user)
templates/macro.html
{% macro hide_email(User) %}
{% set name = User.email.split('@') %}
{% set result = "" %}
{% for i in name[0] %}
{% if loop.index == 0 %}
{%  result += i %}
{% else %}
{%  result += '*' %}
{% endif %}
{% endfor %}
{% result += name[1] %}
<p>{{ result }}</p>
{% endmacro %}

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The first pass to correct the code is to remove the string appending to result, instead simply output the character directly. Added missing at-sign in output

{% macro hide_email(User) %}
{% set name = User.email.split('@') %}
{# set result = "" #}
{# print first char #}{{ name[0][0] }}
{# operate on all the remain name[0] chars #}
{% for i in name[0][1:] %}
{% if loop.index == 0 %}
{#  result += i #}
{% else %}
{#  result += '*' #}*
{% endif %}
{% endfor %}
{# result += name[1] #}@{{ name[1] }}
{#<p>{{ result }}</p>#}
{% endmacro %}

Now after removing the result assignments, the if is not needed since the first character is output directly:

{% macro hide_email(User) %}
{% set name = User.email.split('@') %}
{# print first char #}{{ name[0][0] }}
{# operate on all the remain name[0] chars #}
{% for i in name[0][1:] %}
*
{% endfor %}
@{{ name[1] }}
{% endmacro %}

This produces "k * * * * * * @teamtreehouse.com". The final issue is to remove the default whitespace inserted between elements. This is done by either running all the elements together in a single line, or by using the {%- and -%} {{- and -}} Jinja2 template engine syntax to indicate to remove extraneous whitespace:

{% macro hide_email(User) %}
{% set name = User.email.split('@') %}
{# print first char #}{{ name[0][0] -}}
{#- operate on all the remain name[0] chars -#}
{%- for i in name[0][1:] -%}
*
{%- endfor -%}
@{{ name[1] }}
{% endmacro %}

Running it all together would look like:

{% macro hide_email(User) %}
{% set name = User.email.split('@') %}
{# print first char #}{{ name[0][0] }}{# operate on all the remain name[0] chars #}{% for i in name[0][1:] %}*{% endfor %}@{{ name[1] }}
{% endmacro %}