16/18 Using strings in lists in functions


#1

Hello,
I’ve read through the posts and I think this code is the same as others that work. I do not understand why it is not working.
Thank you

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

print join_strings(n)

join_strings([‘x’, ‘y’, ‘z’, ‘a’]) returned ‘x’ instead of ‘xyza’


#2

Sorry, I did not format my code correctly


'''

n = [“Michael”, “Lieberman”]

Add your function here

def join_strings(words):
result = ""
for word in range(len(words)):
result +=words[word]
return result

print join_strings(n)
’’’

> Blockquote


#3

by default, a function returns None at the end. If we want to return something else at the endo f the function, we can use the return keyword, thus, when the return keyword is reached the function ends

so, your function ends in the first iteration of your loop, given you placed return inside the loop


#4

Nevermind. I erased everything and started the code over again and it works.


#5

but its far more important to understand why the first code you had was failing.


#6

I think there might have been a glitch because when when I copy and paste
code sometimes it doesn’t work. I think I may have used a pasted code
Thank you for your reply


#7

no, this code:

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

print join_strings(n)

has a mistake in it, as i explained here:

i highly recommend solving the problem, it will teach you far more then rewriting from scratch. If you have 1000+ lines, good luck re-writing the whole thing for a single mistake.


#8

Thanks so much. I’ll take that to mind. I will review that error.
Coco


#9

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