Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functional Python Functional Workhorses Map

Andy McDonald
Andy McDonald
5,016 Points

Is a variable with a single letter Truthy?

Still having trouble with this quiz question. My for loop seems to be going but not adding to newword after it becomes what I think is truthy.

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

def reverse(arg):
    letlist = []
    for char in arg:
        letlist.append(char)
    newword = None
    count = len(letlist)
    for i in range(count - 1, -1, -1):
        print('i =' + str(i))
        if newword:
            newword + letlist[i]
        else:
            newword = letlist[i]
    return newword

print(reverse('words'))

1 Answer

A letter is truthy.

The line newword + letlist[i] does not change the value of newword. You could try newword += letlist[i].

With that change, your program would correctly reverse string inputs such as 'tac'. However, if you pass in [5, 4, 3, 2, 1], you will instead get 15, which is the sum of the array, and if you pass in ['t', 'a', 'c'], you will get 'cat' rather than ['c', 'a', 't'].