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

Oziel Perez
Oziel Perez
61,321 Points

Python html macro not working

I'm absolutely stumped, I tried everything I could for this exercise. I have to turn "test@example.com" into "t***@example.com". It says to use a string split and a for loop, which I did use along with list() and enumerate(). I tried to solve this in a python interpreter till I got the string how I wanted it to. So then I put the code in macro syntax and I don't get anything. On top of that, the error says "Try again!" (that is so vaaague). What could I be doing wrong?

lunch.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    class User:
        email = None
    user = User()
    user.email = 'test@example.com'
    return render_template('user.html', user=user)
templates/macro.html
{% macro hide_email(user) %}
    {% email = user.email.split("@") %}
    {% email[0] = list(email[0]) %}
    {% for indx, chr in enumerate(email[0]) %}
        {% if indx > 0 %}
            {% email[0][indx] = "*" %}
        {% endif %}
    {% endfor %}
    {% email[0] = "".join(email[0]) %}
    {% email = "@".join(email) %}
    {{ email }}
{% endmacro %}

1 Answer

This one stumped me for quite a while as well. This code passes the challenge.

  1. Print the first letter
  2. Take the first half of the email split on @ and then slice it the first letter out. For every character in that string print *
  3. print @
  4. print the second half of the email split on @

It is rather vague and easily messed up.

{% macro hide_email(user) %}
  {{ user.email[0] }}{% for char in user.email.split('@')[0][1:] %}*{% endfor %}@{{ user.email.split('@')[1] }}
{% endmacro %}
Oziel Perez
Oziel Perez
61,321 Points

I tried it and it didn't work, it actually spits out "got t***@example.com" (which is supposed to be correct) and somehow it's still wrong. You think I should report this as a bug?

Oziel Perez
Oziel Perez
61,321 Points

never mind it worked. I guess maybe my old answer was cached and still being used, who knows. Thanks!

The one thing that I noticed was keeping it on one line. The code seemed to pick up the line breaks.