7. String in Function. I got the right answer but still confused!



So I was told to write a function called string_function and give it the argument S but no matter what I did, I could not get the right answer. Now it was only after I searched for the answer on here that I toyed with using n as a function instead which did end up giving me the right answer after I used this code below.

I feel like with so many of these exercises, the instructions actually work against me and throw me off, sometimes the examples and templates do too. Please explain to me how I could have used s to get the correct answer instead of using n.

n = "Hello"
def string_function(n):
    s = 'world'
    return n + s 

print (string_function(n))


We would expect output like this,

Hello world

To save confusion when writing functions, use a unique variable name. It won't make any difference in this case, but it will help to clarify the change in scope.

def string_function(s):
    return s + " world"

n = "Hello"
print (string_function(n))


Hi @burnoutbiograd ,

Sorry that the instructions and examples are not always sufficiently clear.

Below is a version of string_function with comments to explain how the two lines relate to the instructions.

From Instructions:

  • Write a function called string_function that takes in a string argument (s) and then returns that argument concatenated with the word 'world'.

def string_function(s): # string_function with an argument, s
    return s + 'world'  # return argument, s, concatenated with 'world'


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