Strings in Functions


#1

n = "Hello"
def string_function(x):
return n + "world"
print string_function(n)

Why the message
"Oops, try again. string_function('Waynes') returned 'Helloworld' instead of 'Waynesworld'"?!


#2

Here you need a space after Hello

so instead you write it like,

n = "Hello "

And here,

You should be concatenating your parameter x with world not the global variable n. :slight_smile:


#3

Actually, you need to get "Helloworld" all together, not "Hello world". I experimented with my code and this one works for me :

n = "Hello"

def string_function(n):
abc = "world"
return n + abc

print string_function(n)

I hope that it gets the job done, good luck :slight_smile:


#5

Look at the argument for the function. That argument needs to be in the function somewhere if it's returning something. Instead, you're calling the global variable 'n' instead of the function variable 'x'. instead of using 'return n + "world'', use 'return x + "world"'.

I was having the same issue then had a "Wow, how did I miss that?" moment.


#6

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