Using strings in lists in functions, code seems right, but still gives error


Traceback (most recent call last):
File "python", line 9, in
File "python", line 6, in join_strings
TypeError: unsupported operand type(s) for +: 'int' and 'str'

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for i in range(len(words)):
        result = i + result 
    return result

print join_strings(n)

It looks correct, but it's not for some reason


Don't use range.
Using range, i is being set to an int instead of the string.
Replace this:
for i in range(len(words)):
With this:
for i in words:


You will also need to have result first in the concatenation as otherwise it will put your words out of order.

result = result + i

instead of:

result = i + result


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.