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 Functional Python Functional Workhorses Map

Code Challenge: Map

For the code challenge: "Create a function named "reverse" which takes a single iterable as an argument, and returns a reversed version of that item."

I have entered this code into workspaces and the terminal--it works perfectly fine in both places. However, the code challenge keeps giving me a "Bummer! reverse didn't accept an argument."

For the life of me, I cannot figure out what's wrong here.

maps.py
backwards = [
    'tac',
    'esuoheerT',
    'htenneK',
    [5, 4, 3, 2, 1],
]

def reverse(i):
    i = iter(i)
    idx = len(i)
    i2 = ""
    while idx:
        idx -= 1
        i2 += i[idx]
    return i2

1 Answer

Figured it out, for anyone interested or also struggling--The code above WILL reverse strings and integers...however, something the test code is throwing at it is not compatible.

The correct solution was to use a slice, which steps backward through the iterable and returns the reverse:

def reverse(iterable):
     return iterable[::-1]