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

Paul Bentham
Paul Bentham
24,090 Points

Hide_email() Flask, can anybody help identify my errors in this one?

So I have this standard Python code that works:

first = email.split('@')[0]
second = email.split('@')[1]

print first
print second

hidden = ""

for i in range(0,len(first)):
    if i == 0:
            hidden += first[i]
    else:
        hidden += '*'

print hidden            

new = hidden+'@'+second
print new

But when I try and convert this to Jinja the question is not passing?

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) %}
    {{ first = User.email.split('@')[0] }}
    {{ second = User.email.split('@')[1] }}
    {{ hidden = "" }}

    {%- for i in range(0,len(first)) -%}
      {%- if i == 0 -%}
        {{ hidden += first[i] }}
      {%- else -%}
        {{ hidden += '*' }}
      {%- endif -%}
    {%- endfor -%}
    {{ new = hidden+'@'+second }}
    <p>{ new }</p>
{% endmacro %}

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Not all Standard Python can be used directly within jinja template syntax. Specifically, the "+=" and variable assignment.

Instead of accumulating the characters in a string, simply output them as you go. And using the "minus" syntax you won't have the extra whitespace.

If you need more help, check out the other solutions to this challenge here.