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 trialAndreas cormack
Python Web Development Techdegree Graduate 33,011 Pointscreate a macro hide_email
this code logic runs in the python shell fine but cannot get it to work on this challenge anyone any ideas.
jinja2
{% macro hide_email(User) %}
{% for User in Users %}
{% before_the_at,after_the_at=User.email.split("@",1) %} #split the string on the @ sign """
{% after_the_at="@"+after_the_at %} #all the @ sign to the second half of the string """
{% email_format=""%} # setting a variable called email_format
{% for letter_count in range(0,len(before_the_at)) %} # looping through each letter in the string before_the_at
{% if letter_count==0 %}# if the count is 0 or first letter add that to the string email_format
{% email_format=before_the_at[0]%}
{% else %}# else add **** to the end of the email_format string
{% email_format=email_format+"*" %}
{% endfor %}
{% email_format=email_format+after_at %} # lastly appending the after_at string to the email format
{{% email_format %}}
{% endfor %}
{% endmacro %}
3 Answers
Dan Johnson
40,533 PointsThe embedded Python in Jinja2 has some syntax differences. For a solution similar to what you're going for:
{# Only one User is being sent in #}
{% macro hide_email(User) %}
{#- Use "set" to declare a variable -#}
{%- set before_the_at, after_the_at = User.email.split("@") -%}
{%- set after_the_at = "@" + after_the_at -%}
{#- Use the "length" filter instead of the "len" function -#}
{%- for letter_count in range(before_the_at|length) -%}
{%- if letter_count == 0 -%}
{#- Attempting to assign to something set in an outer block will just shadow it with another variable. -#}
{{before_the_at[0]}}
{%- else -%}
{{"*"}}
{%- endif -%}
{%- endfor -%}
{{after_the_at}}
{%- endmacro %}
I know the challenge mentions using a for loop, but if you want to use "set" you can shorten it to this:
{% macro hide_email(user) %}
{%- set handle, domain = user.email.split("@") -%}
{%- set hidden = handle[0] + ("*" * handle[1:]|length) -%}
{{ hidden + "@" + domain }}
{%- endmacro %}
james white
78,399 PointsHi Dan,
Thanks.
Your 'set' code was much more helpful than the information given in this other thread on the same challenge:
Tom Drury
10,546 PointsCan someone explain the difference between using {% ... %} and {%- ... -%}. It seems to make a difference. thanks.
Dan Johnson
40,533 PointsAdding the minus sign will remove whitespace from the generated HTML before or after (or both if you add the minus sign to both sides) the expression/block.
The template documentation under the "Whitespace Control" section has more info: http://jinja.pocoo.org/docs/dev/templates/#whitespace-control
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsAndreas cormack
Python Web Development Techdegree Graduate 33,011 PointsThanks Dan that's was were helpful.
Nadine Abella
1,032 PointsNadine Abella
1,032 PointsDan Johnson what does the
|
do?
Dan Johnson
40,533 PointsDan Johnson
40,533 PointsThe | operator applies a filter, a modifier for the variable on the left. You can go here for a list of all built-in filters.