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 trialVittorio Somaschini
33,371 PointsMacro challenge, something is not right in my code
Hello everybody.
As you may guess I am struggling with the macro challenge.
First of all here is my code:
{% macro hide_email(User) %}
{% split_email = user.email.split('@') %}
{% hidden_email = split_email[0][0] %}
{% for letter in split_email[0][1:]: %}
{% hidden_email += '*' %}
{% endfor %}
{{ hidden_email + '@' + split_email[1] }}
{% endmacro %}
I am pretty positive I am getting the right value for the user.email as I have tested the steps in the terminal but I am not able to pass the code challenge.
I am not 100% sure but I think it has to be something in the connection between files: what I mean by this is if I need to change something else in the other file or maybe using wrong names in my code.
I have watched the video a few times now but unfortunately since we do not have access to the user.html file I can't really figure out where I am going wrong here.
Any idea / hints ?
Much appreciated Ty
Vittorio
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)
{% macro hide_email(User) %}
{% split_email = user.email.split('@') %}
{% hidden_email = split_email[0][0] %}
{% for letter in split_email[0][1:]: %}
{% hidden_email += '*' %}
{% endfor %}
{{ hidden_email + '@' + split_email[1] }}
{% endmacro %}
3 Answers
james white
78,399 PointsHi Vittorio,
I assume you are dealing with the Macros challenge:
http://teamtreehouse.com/library/build-a-social-network-with-flask/takin-names/macro
..and are trying to find out what code you need to put in the "templates/macro.html" tab.
Have you looked at this other forum thread dealing with the same challenge:
https://teamtreehouse.com/forum/create-a-macro-hideemail
I found Dan Johnson's answer code (which uses 'set' to bypass the need to use a for...endfor loop) to be especially helpful:
{% macro hide_email(user) %}
{%- set handle, domain = user.email.split("@") -%}
{%- set hidden = handle[0] + ("*" * handle[1:]|length) -%}
{{ hidden + "@" + domain }}
{%- endmacro %}
Vittorio Somaschini
33,371 PointsHello James.
Ty for your reply.
Yes I am dealing with that challenge and I have seen the other 2 threads related to it.
I pretty much understand those, but I can't figure out what I did wrong as I think my code delivers the same output...
The code challenge does not accept it as good, so I went wrong somewhere and would like to understand where.
;)
Vittorio
Kenneth Love
Treehouse Guest TeacherThe challenge has been updated to show the macro output in the preview pane. Hopefully that'll help you see what's going on.
Vittorio Somaschini
33,371 PointsThanks Kenneth.
That helps out a lot in understanding that the syntax changes in jinja2.
At least this is what I understood.
Well done, I think you deserve more than 7000 points
:-D
Vittorio