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 trialVictor Gavojdea
11,796 PointsWhy do I need a for loop to print t***@example.com
I can't even begin to solve this because the for loop requirement is throwing ME for a loop (haha, seriously though, I don't even.)
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 User %}
{% email = User.email.split('@') %}
{% for %}
{% endfor %}
{% endmacro %}
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsThe idea behind using a loop is to print an asterisk for each character in the email name after the first character. You can peruse the other questions on this challenge to see how loops were used.
You pose an interesting question: can this challenge be solved without using a loop?
Yes! It can be by using length
filter to get the number of asterisks needed.
- {{ email[0][0] }}{# first char #}
- {{ "*" * email[0][1:]|length }}{# replace remainder with '*' #}
- @{# at-sign #}
- {{ email[1] }}{# domain name #}
Putting them all together will yield the answer.
Also, the first line of the macro needs the macro name and parameter list:
- {% macro hide_email(User) %}
Bonus: if you need to remove whitespace between printed elements, add a minus ("-") to either (or both) of the delimiters:
- {{ --> {{- and }} --> -}}
- {% --> {%- and %} --> -%}
Victor Gavojdea
11,796 PointsVictor Gavojdea
11,796 PointsIncredibly helpful. Thank you!