Using strings in lists in functions


Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned 'xy' instead of 'xyza'

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

print join_strings(n)


Youre not taking advantage of for loop.
also its important to note that your function should able to deal any lists not just n.

an example will make things clear..

suppose there is list of numbers..

x = [1,2,3,4]

and I want to find sum of all numbers, How do I use for loop to do this?

sum = 0
for i in x:
    sum = sum + i

print sum # This will print 10

same way you can make some changes here..

result = words[0] + words[1]


Okay I understand it now. Thank you.


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