Bug With String and Functions


#1

I don't know if this is just me being stupid, but this seems like a bug

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


print join_strings(n)

And it tells me this: Oops, try again.
join_strings(['x', 'y', 'z', 'a']) returned ' xyza' instead of 'xyza'
If this ain't a bug and just my code is wrong, please tell me how to fix it


#2

No bug In the exercise but the code has bug, while defining the empty string,you defined a string having one space character


#3

How exactly did I do that?


#4

Have you defined result string with a single space character ?
You can see its not empty but it has a length of 1.

If yes and you don't want to change it ,Then slice is one method to get rid of it from final string.

Or

Effectively, you can define it as an empty string. (Then no need of slicing)


#5

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