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 trialmartinhenoch
3,340 PointsProblem using 'len' function to print correct number of asterisks.
I'm trying to use this line:
{{ user.email[0] + '*'*(len(user.email.split('@')[0])-1) + '@' + user.email.split('@')[-1] }}
as seen in templates/macro.html. It seems like everything works except for printing out the asterisks. I'm at a loss what to do..
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) %}
{{ user.email[0] + '*'*(len(user.email.split('@')[0])-1) + '@' + user.email.split('@')[-1] }}
{% endmacro %}
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsA very interesting approach! The issue is the Python len()
function isn't available in the templates. However there is a filter available that will give you what you want. Modifying your macro:
{% macro hide_email(user) %}
{{ user.email[0] + '*'* ((user.email.split('@')[0] | length)-1) + '@' + user.email.split('@')[-1] }}
{% endmacro %}
martinhenoch
3,340 Pointsmartinhenoch
3,340 PointsThis solution worked, thank you!