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 Customizing Django Templates Building Custom Filters Reverse Text Filter

Tried everything i could think of...HELP!

Create a function called "reverse_text" that takes a string as an argument, and returns a string with those words reversed. So reverse_text("balloon") should return "noollab".

code_challenges/templatetags/extras.py
from django import template

register = template.Library()

def reverse_text(agr):
    return template("noollab")

4 Answers

def reverse_text(string): return string[::-1]

this will pass the challenge

Steven Parker
Steven Parker
231,248 Points

The challenge is only telling you that reverse_text("balloon") should return "noollab" as an example.

You should not assume what actual argument will be given, and your function should create the output by using the argument and performing a process on it that will give the desired result.

i did the following code and its not working.

from django import template

register = template.Library()

def reverse_text(balloon): return "noollab"

Steven Parker
Steven Parker
231,248 Points

You're still returning a fixed string that would only be the proper response if the function was called with "balloon" as an argument. But what if it was called with "widget" or "jabberwock?

You need to compute a reversed version of the argument so the answer will be correct no matter what the input is.

how Did you solve this one, I am facing a similar challenge ,

Steven Parker
Steven Parker
231,248 Points

There are a few ways to do this, one would be to use the "reversed" option on a loop.
But a slice with a negative step value is even easier. :wink:

you should return the answer as below

def reversed_text("balloon"): return balloon[::-1]

it should pass the test

Steven Parker
Steven Parker
231,248 Points

That's what I was hinting at a year and a half ago. I was just trying to avoid giving away a "spoiler".