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

Jason Brown
Jason Brown
6,878 Points

Shouldn't this pass? I don't use a for loop but what's wrong with this?

{% macro hide_email(user) %} {% name, domain = user.email.split('@') %} {% hidden = name[0] + ''(len(name)-1) + '@' + domain %} {{ hidden }} {% 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) %}
  {% name, domain = user.email.split('@') %}
  {{ name[0] + '*'*(len(name)-1) + '@' + domain }}
{% endmacro %}

4 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Jason.

If you click on preview in the code challenge, (left from the check work button) you will notice that jinja2 is complaining about something. (according to my experience jinja2 complains a lot :) )

The fact is that, as far as I know the syntax in jinja2 differs from the plain python. Your idea for the code challenge is nice actually, but you need to integrate a couple of things.

  1. You need to put the second and 3rd line inside a with block, so saying {% with name, domain ... %}

End then close the with block right above the last line of code you outputted.

  1. the normal length method does not seem to work inside jinja2, so instead you gotta use name | length to get the length of name; you may want to have a look at this link as it explains it much better than I did: http://stackoverflow.com/questions/24163579/length-of-string-in-jinja-flask

It should then pass the challenge, let me know if still troubles

Vittorio

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Pete.

If you click the preview button (left from check work), it may give you an idea of what is not going right here (scroll down to the bottom).

Also, I notice a couple of things you need to sort out here:

1 at the end, in flask (or better jinja2) we print things using {{ whatever }}, so the second last line does not have to have "print", but it only needs to have a set of {{ }}

2 Going with Jason's solution (which is quiet nice I think), we can also get rid of the for loop as we just use a multiply there and things get even easier, so (in this case) you do not need to create the n list as the newly created name variable is all you need in order to have the first letter and the number of '*' required to hide the email address.

The last thing is that in this case you would need to pay attention to the syntax: in jinja2 syntax is a little bit different than python's pure syntax. So as recommended to Jason, we need to surround the code with a with block.

Vittorio

Pete Jeffryes
Pete Jeffryes
5,752 Points

Did Vittorio's fix work? I have been working on this this morning. Your code is a nice and concise solution, mine was lines long...

I was thinking when you split the User.email you end up with the name as a string. You may need to make a list of each character in name using a for loop. Then you will have access to name[0].

Pete Jeffryes
Pete Jeffryes
5,752 Points

This is my version of Jason's solution, but it isn't working either. I tried using the standard len() as well, no joy. Any ideas?

{% macro hide_email(User) %}

  {% name, domain = User.email.split('@')%}
  {% n = [] %}
  {% for l in name %}
    {% n.append(l) %}
  {% endfor %}
  {% hidden = (n[0] + ('*' * ((n|length)-1) + '@' + domain) %}
  {% print hidden %}
{% endmacro %}