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 trialAndy McDonald
Python Development Techdegree Graduate 13,801 PointsIs 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.
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
jb30
44,807 PointsA 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']
.