Chapter 7 - Strings in functions 7/18: Found a bug?



I think I found a bug in this exercise.

Codeacademy gave this answer, which doesnt make sense to me, and I think it should be s instead of n.

n = "Hello"
# Your function here!
def string_function(s):
	return s + 'world'

print string_function(n)

When I insert s everywhere, it still works!

s = "Hello"
# Your function here!
def string_function(s):
	return s + 'world'

print string_function(s)

Is it a bug or is there something I don’t see?


technically speaking, both solutions are correct

However, the first one is better practice because it avoids variable naming confusion

first lets understand parameters vs arguments:

# function with parameter s
def string_function(s):
    return s + 'world'

# function call with string argument
print string_function("hello")

see the comments i included, i also removed the variable and passed the string as argument on function call to clear some confusion.

so the argument at function call gets passed to function parameter, so this can be a string, or a variable containing a string.

Now, its better to not have same variable name for argument and parameter, this might confusing (scope of the variables for example)


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