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

Chris Sehnert
Chris Sehnert
30,857 Points

hidden email format

The following code returns: k******Teamtreehouse.com ...which I thought was correct ...but answer is unaccepted...

{% macro hide_email(User) %} {% with atSymbolIndex = User.email.find('@'[:]) %} {{ User.email[0] }} {% for letter in User.email[1:atSymbolIndex] %} * {% endfor %} {% for letter in User.email[atSymbolIndex:] %} {{ letter }} {% endfor %} {% endwith %} {% endmacro%}

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) %}
    {% with atSymbolIndex = User.email.find('@'[:]) %}
    {{ User.email[0] }}
    {% for letter in User.email[1:atSymbolIndex] %}
        *
    {% endfor %}
    {% for letter in User.email[atSymbolIndex:] %}
        {{ letter }}
  {% endfor %}
    {% endwith %}
{% endmacro%}

2 Answers

Your code is leaving white space between letters. Check out Jinja2 Whitespace-control

Chris Sehnert
Chris Sehnert
30,857 Points

I came up with a way to accomplish the task without using a for loop, thus preventing the whitespace....even though the original question suggests using a for loop....

{% macro hide_email(User) %}
    {% with asterisks_count = User.email.find('@') - 1 %}
    {% with hidden_email = [User.email[0], '*' * asterisks_count, User.email[asterisks_count + 1:]] %}
    {{ "".join(hidden_email) }}
    {% endwith %}
    {% endwith %}
{% endmacro %}